首页 » 技术分享 » android系统自定义关机界面

android系统自定义关机界面

 

自定义的dialog

 
package com.android.internal.policy.impl;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.os.UserHandle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;
import android.app.Dialog;
import android.os.Bundle;
import android.os.IPowerManager;
import android.os.ServiceManager;
import android.os.RemoteException;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.text.TextUtils;
import android.view.MotionEvent;
import com.android.internal.R;
import android.view.WindowManagerPolicy.WindowManagerFuncs;

public class ShutdownDialog extends Dialog implements OnClickListener {

	private View mDayIcon;
	private ImageView mNightIcon;
	private ImageView mTshIcon;
	private ImageView mPowerIcon;
	private ImageView mShDIcon;
	private ImageView mLightyIcon;
	private ImageView mLightsIcon;
	private ImageView mLighteIcon;
	private TextView mRemindTv;
	private Context mContext;
	private AudioManager aduioManager;
	private static final String OPEN_FLIP_BRIGHT_SCREEN = "com.android.open.flipbrightscreen";
	private static final String CLOSE_FLIP_BRIGHT_SCREEN = "com.android.close.flipbrightscreen";
    private WindowManagerFuncs mWindowManagerFuncs;
	private boolean mCancelOnUp;
	private int mLastMotionY;
	private Dialog mDialog = null;
	private static final int CLOSE_DIALOG = 1001;
	private MyHandler mHandler;
	private boolean isDownPower = false;
	
	ShutdownDialog(Context context){
		this(context,R.style.shutdown_dialog);
		mContext = context;
	}
	
	ShutdownDialog(Context context,WindowManagerFuncs windowManagerFuncs){
		this(context,R.style.shutdown_dialog);
		mContext = context;
		mWindowManagerFuncs = windowManagerFuncs;
	}
	
	ShutdownDialog(Context context,int style){
		super(context,style);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shutdown_dialog);
		WindowManager m = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
		Display d = m.getDefaultDisplay();
		LayoutParams params = getWindow().getAttributes();
		params.height = (int) (d.getHeight() * 1.005);
		params.width = (int) (d.getWidth() * 1.005);
		this.getWindow().setAttributes(params);
		mHandler = new MyHandler();
		initData();
		init();
	}

	private void initData() {
		aduioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
	}

	private void init() {
		mDayIcon = (ImageView)findViewById(R.id.normal_mode);
		mDayIcon.setOnClickListener(this);
		mNightIcon = (ImageView)findViewById(R.id.mute_mode);
		mNightIcon.setOnClickListener(this);
		mTshIcon = (ImageView)findViewById(R.id.conspire_bright_screen);
		mTshIcon.setOnClickListener(this);
		mPowerIcon = (ImageView)findViewById(R.id.powr_icon);
		mPowerIcon.setOnClickListener(this);
		mShDIcon = (ImageView)findViewById(R.id.shd_icon);
		mShDIcon.setOnClickListener(this);
		mLightyIcon = (ImageView)findViewById(R.id.lighty_icon);
		mLightyIcon.setOnClickListener(this);
		mLighteIcon = (ImageView)findViewById(R.id.lighte_icon);
		mLighteIcon.setOnClickListener(this);
		mLightsIcon = (ImageView)findViewById(R.id.lights_icon);
		mLightsIcon.setOnClickListener(this);
		mRemindTv = (TextView)findViewById(R.id.remind_tv);
		refreshStatus();
	}

	private void refreshStatus() {
		if(getModeStyle()){
			mNightIcon.setBackgroundResource(R.drawable.ic_mute_mode_on);
		}else {
			mDayIcon.setBackgroundResource(R.drawable.ic_normal_mode_on);
		}
		boolean isFlipBrightScreen = (Settings.System.getInt(mContext.getContentResolver(),Settings.System.FLIP_BRIGHT_SCREEN, 0) != 0);
		if (isFlipBrightScreen) {
			mTshIcon.setBackgroundResource(R.drawable.ic_conspire_on);
		} else {
			mTshIcon.setBackgroundResource(R.drawable.ic_conspire_off);
		}
		
		if(getScreenBrightness()<85){
			mLightyIcon.setBackgroundResource(R.drawable.ic_light_on1);
		}else if (85<getScreenBrightness()&&getScreenBrightness() < 196){
			mLighteIcon.setBackgroundResource(R.drawable.ic_light_on2);
		}else{
			mLightsIcon.setBackgroundResource(R.drawable.ic_light_on3);
		}
		
		boolean isPowerAction = Settings.System.getInt(mContext.getContentResolver(),Settings.System.POWER_SAVING, 0) == 1;
		Log.d("hzr", "ShutdownDialog --isPowerAction  ="+isPowerAction);
		if (isPowerAction) {
			mShDIcon.setBackgroundResource(R.drawable.ic_shd_on);
		} else {
			mShDIcon.setBackgroundResource(R.drawable.ic_shd_off);
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.normal_mode:
			showToast(mContext.getString(R.string.zz_nol_mode));
			AudioManager mdAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
			mdAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
			mNightIcon.setBackgroundResource(R.drawable.ic_mute_mode_off);
			mDayIcon.setBackgroundResource(R.drawable.ic_normal_mode_on);
			mHandler.sendEmptyMessageDelayed(CLOSE_DIALOG, 2000);
			mContext.sendBroadcast(new Intent("com.android.day.dismiss")); 
			break;
		case R.id.mute_mode:
			showToast(mContext.getString(R.string.zz_sil_mode));
			AudioManager mnAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
			mnAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			mDayIcon.setBackgroundResource(R.drawable.ic_normal_mode_off);
			mNightIcon.setBackgroundResource(R.drawable.ic_mute_mode_on);
			mHandler.sendEmptyMessageDelayed(CLOSE_DIALOG, 2000);
			mContext.sendBroadcast(new Intent("com.android.night.dismiss")); 
			break;
		case R.id.powr_icon:
			Settings.System.putInt(mContext.getContentResolver(),"ShutDownDialog", 0);
			Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
			shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
			shutdown.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_NEW_TASK);
			mContext.startActivity(shutdown);
			break;
		case R.id.shd_icon:
			if(isDownPower)
				return;
			boolean isPowerAction = Settings.System.getInt(mContext.getContentResolver(),Settings.System.POWER_SAVING, 0) == 1;
			if (isPowerAction) {
				isDownPower = true;
				showToast(mContext.getString(R.string.cancel_power_saving));
				Intent mclosePowerAction = new Intent();
				mclosePowerAction.setAction("android.intent.action.closePowerAction");
				mContext.sendBroadcast(mclosePowerAction);
				mShDIcon.setBackgroundResource(R.drawable.ic_shd_off);
			} else {
				isDownPower = true;
				showToast(mContext.getString(R.string.open_power_saving));
				Intent mopenPowerAction = new Intent();
				mopenPowerAction.setAction("android.intent.action.openPowerAction");
				mContext.sendBroadcast(mopenPowerAction);
				mShDIcon.setBackgroundResource(R.drawable.ic_shd_on);
				mContext.sendBroadcast(new Intent("com.android.action.manual.operation"));
			}
			mHandler.sendEmptyMessageDelayed(CLOSE_DIALOG, 2000);
			break;
		case R.id.conspire_bright_screen:
			boolean isFlipBrightScreen = (Settings.System.getInt(mContext.getContentResolver(),Settings.System.FLIP_BRIGHT_SCREEN, 0) != 0);
			if (!isFlipBrightScreen) {
				showToast(mContext.getString(R.string.open_tsh));
				Settings.System.putInt(mContext.getContentResolver(), Settings.System.FLIP_BRIGHT_SCREEN, 1);
				mContext.sendBroadcastAsUser(new Intent(OPEN_FLIP_BRIGHT_SCREEN), UserHandle.ALL);
				mTshIcon.setBackgroundResource(R.drawable.ic_conspire_on);
			} else {
				showToast(mContext.getString(R.string.cancel_tsh));
				Settings.System.putInt(mContext.getContentResolver(), Settings.System.FLIP_BRIGHT_SCREEN, 0);
				mContext.sendBroadcastAsUser(new Intent(CLOSE_FLIP_BRIGHT_SCREEN), UserHandle.ALL);
				mTshIcon.setBackgroundResource(R.drawable.ic_conspire_off);
			}
			mHandler.sendEmptyMessageDelayed(CLOSE_DIALOG, 2000);
			break;
		case R.id.lighty_icon:
			showToast(mContext.getString(R.string.light_power_select));
			setBrightness(70);
			mLightyIcon.setBackgroundResource(R.drawable.ic_light_on1);
			mLighteIcon.setBackgroundResource(R.drawable.ic_light_off2);
			mLightsIcon.setBackgroundResource(R.drawable.ic_light_off3);
			mHandler.sendEmptyMessageDelayed(CLOSE_DIALOG, 2000);
			mContext.sendBroadcast(new Intent("com.android.brightenes.dismiss")); 
			break;
		case R.id.lighte_icon:
			showToast(mContext.getString(R.string.light_power_select));
			setBrightness(195);
			mLighteIcon.setBackgroundResource(R.drawable.ic_light_on2);
			mLightyIcon.setBackgroundResource(R.drawable.ic_light_off1);
			mLightsIcon.setBackgroundResource(R.drawable.ic_light_off3);
			mHandler.sendEmptyMessageDelayed(CLOSE_DIALOG, 2000);
			mContext.sendBroadcast(new Intent("com.android.brightenes.dismiss")); 
			break;
		case R.id.lights_icon:
			showToast(mContext.getString(R.string.light_power_select));
			setBrightness(255);
			mLightsIcon.setBackgroundResource(R.drawable.ic_light_on3);
			mLightyIcon.setBackgroundResource(R.drawable.ic_light_off1);
			mLighteIcon.setBackgroundResource(R.drawable.ic_light_off2);
			mHandler.sendEmptyMessageDelayed(CLOSE_DIALOG, 2000);
			mContext.sendBroadcast(new Intent("com.android.brightenes.dismiss")); 
			break;

		default:
			break;
		}
	}

	// 判断是否勿扰模式
	private boolean getModeStyle() {
		if (aduioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
			return true;
		} else {
			return false;
		}
	}
	
	// 设置屏幕亮度 亮度值,值为0至100
	public void setBrightness(int brightness) {

		try {
			IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
			if (power != null) {
				if (power.isInteractive()) {
					power.setTemporaryScreenBrightnessSettingOverride(brightness);
					Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
				} else {
					power.setTemporaryScreenBrightnessSettingOverride(-1);
				}
			}
		} catch (RemoteException doe) {
		}
	}

	/**
	* 获得当前屏幕亮度值 0--255
	*/
	private int getScreenBrightness() {
		int screenBrightness = 255;
		try {
			screenBrightness = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
		} catch (Exception localException) {

		}
		return screenBrightness;
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		int y = (int) ev.getRawY();
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mLastMotionY = y;
			break;
		case MotionEvent.ACTION_MOVE:
			int deltaY = y - mLastMotionY;
			Log.d("hzr","deltaY ="+deltaY+"  ,mLastMotionY ="+mLastMotionY);
			if (deltaY > 50 && mLastMotionY < 100) {
                mCancelOnUp = true;
            }
			break;
		case MotionEvent.ACTION_UP:
			if (mCancelOnUp) {
                cancel();
            }
            mCancelOnUp = false;
			break;
		}
		return super.dispatchTouchEvent(ev);
	}
	
	private void showToast(String str) {
		mRemindTv.setText(str);
		mRemindTv.setVisibility(View.VISIBLE);
		//Toast toast = Toast.makeText(mContext, str, Toast.LENGTH_SHORT);
		//toast.setGravity(Gravity.CENTER, 0, 0);
		//toast.show();
		Log.d("hzr","showToast"+str);
	}
	
	class MyHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case CLOSE_DIALOG:
				//if (mDialog != null)
				//	mDialog.dismiss();
				//mDialog = null;
				if(isDownPower == true)
					isDownPower = false;
				mRemindTv.setVisibility(View.INVISIBLE);
				break;
			default:
				break;
			}
		}
	}
}

package com.android.internal.policy.impl;

import android.app.StatusBarManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.os.SystemProperties;
import android.net.Uri;
import android.database.ContentObserver;
import android.util.Log;
import android.view.View;
import android.provider.Settings;
import android.view.ViewGroup;
import android.view.WindowManager;
import com.android.internal.R;
import android.view.WindowManagerPolicy.WindowManagerFuncs;

/**
 * Helper to show the global actions dialog.  Each item is an {@link Action} that
 * may show depending on whether the keyguard is showing, and whether the device
 * is provisioned.
 */
class ShutdownActions implements DialogInterface.OnDismissListener{

    private static final String TAG = "ShutdownActions";
    private final Context mContext;
    private ShutdownDialog mDialog;
    private boolean mKeyguardShowing = false;
	private boolean mDeviceProvisioned = false;
	static private boolean mIsVisable = false;
	private boolean isGlobalActions;
	private WindowManagerFuncs mWindowManagerFuncs;
	private static final int MESSAGE_DISMISS = 0;
	private static final int MESSAGE_SHOW = 2;
	
	private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGE_DISMISS:
                if (mDialog != null) {
                    mDialog.dismiss();
                }
                break;
            case MESSAGE_SHOW:
                handleShow();
                break;
            }
        }
    };
	
	public ShutdownActions(Context context,WindowManagerFuncs windowManagerFuncs) {
        mContext = context;
		mWindowManagerFuncs = windowManagerFuncs;
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        context.registerReceiver(mBroadcastReceiver, filter);
    }
	
	static public boolean isVisable() {
        return mIsVisable;
    }
	
	public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
        mKeyguardShowing = keyguardShowing;
        mDeviceProvisioned = isDeviceProvisioned;
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
            mHandler.sendEmptyMessage(MESSAGE_SHOW);
        } else {
            handleShow();
        }
		Settings.System.putInt(mContext.getContentResolver(),"ShutDownDialog", 1);
    }

    private ShutdownDialog createDialog() {
        final ShutdownDialog dialog = new ShutdownDialog(mContext,mWindowManagerFuncs);
		dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
        dialog.setOnDismissListener(this);
        return dialog;
    }
	
	private void handleShow() {
        mDialog = createDialog();
        prepareDialog();
        WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
        attrs.setTitle("GlobalActions");
        mDialog.getWindow().setAttributes(attrs);
        mDialog.show();
        mDialog.getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_DISABLE_EXPAND);
		mIsVisable = true;
    }
	
	public void onDismiss(DialogInterface dialog) {
		Settings.System.putInt(mContext.getContentResolver(),"ShutDownDialog", 0);
        mIsVisable = false;
		isGlobalActions = false;
		mContext.sendBroadcast(new Intent("com.android.shutdown.dismiss")); 
    }
	
	private void prepareDialog() {
        mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
    }
	
	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action) || Intent.ACTION_SCREEN_OFF.equals(action)) {
                String reason = intent.getStringExtra(PhoneWindowManager.SYSTEM_DIALOG_REASON_KEY);
                if (!PhoneWindowManager.SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
                    mHandler.sendEmptyMessage(MESSAGE_DISMISS);
                }
            }
        }
    };
}

dialogXML布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shutdown_bg" >
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="30dip"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/normal_mode"
            android:layout_width="68dp"
            android:layout_height="53dp"
            android:clickable="true"
			android:layout_alignParentLeft="true"
            android:layout_marginLeft="110dp"
            android:src="@drawable/ic_normal_mode_off" />
        <ImageView
            android:id="@+id/mute_mode"
            android:layout_width="68dp"
            android:layout_height="53dp"
            android:clickable="true"
			android:layout_alignParentRight="true"
            android:layout_marginRight="110dp"
            android:src="@drawable/ic_mute_mode_off" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:paddingLeft="32dp"
        android:layout_marginTop="110dp"
        android:paddingRight="32dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/conspire_bright_screen"
            android:layout_width="92dp"
            android:layout_height="92dp"
			android:layout_alignParentLeft="true"
            android:clickable="true" />

        <ImageView
            android:id="@+id/powr_icon"
            android:layout_width="92dp"
            android:layout_height="92dp"
			android:layout_marginLeft="10dp"
			android:layout_toRightOf="@+id/conspire_bright_screen"
            android:clickable="true"
            android:src="@drawable/ic_lock_power_selector" />

        <ImageView
            android:id="@+id/shd_icon"
            android:layout_width="92dp"
            android:layout_height="92dp"
			android:layout_marginLeft="10dp"
			android:layout_toRightOf="@+id/powr_icon"
            android:clickable="true" />
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="82dip"
        android:paddingLeft="80dp"
		android:paddingRight="43dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/lighty_icon"
            android:layout_width="80dp"
            android:layout_height="41dp"
            android:clickable="true"
			android:layout_alignParentLeft="true"
            android:src="@drawable/ic_light_off1" />
        <ImageView
            android:id="@+id/lighte_icon"
            android:layout_width="80dp"
            android:layout_height="41dp"
            android:clickable="true"
			android:layout_toRightOf="@+id/lighty_icon"
            android:src="@drawable/ic_light_off2" />
        <ImageView
            android:id="@+id/lights_icon"
            android:layout_width="80dp"
            android:layout_height="41dp"
            android:clickable="true"
			android:layout_toRightOf="@+id/lighte_icon"
            android:src="@drawable/ic_light_off3" />
    </RelativeLayout>
	
	<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="32dip"
        android:orientation="horizontal"
        android:paddingLeft="80dp"
        android:paddingRight="80dp" >

        <TextView
            android:id="@+id/remind_tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
			android:singleLine="true"
            android:textColor="@android:color/white"
            android:textSize="16sp" />
    </LinearLayout>

</RelativeLayout>

设置dialog的样式

<style name="shutdown_dialog" parent="android:style/Theme.Black.NoTitleBar.Fullscreen">
        <item name="android:windowNoTitle">true</item>
		<item name="android:windowFullscreen">true</item>
    </style>

资源文件,图片,样式,布局,字符串  要在values的public.xml里配置。资源ID在values的symbols.xml里配置()

dialog好了之后在PhoneWindowManager.java(frameworks\base\policy\src\com\android\internal\policy\impl)里调用

72的把showGlobalActionsDialog();替换自己自定义的showShutdownDialog();

ShutdownActions mShutdownActions;
    void showShutdownDialog() {
        if (mShutdownActions == null) {
            mShutdownActions = new ShutdownActions(mContext,mWindowManagerFuncs);
        }
        final boolean keyguardShowing = keyguardIsShowingTq();
        mShutdownActions.showDialog(keyguardShowing, isDeviceProvisioned());
		if (keyguardShowing) {
            mPowerManager.userActivity(SystemClock.uptimeMillis(), false);
        }
    }

80的的把powerLongPress()里的showGlobalActionsInternal();替换自己自定义的showShutdownDialog();

ShutdownActions mShutdownActions;
    void showShutdownDialog() {
		sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
        if (mShutdownActions == null) {
            mShutdownActions = new ShutdownActions(mContext,mWindowManagerFuncs);
        }
        final boolean keyguardShowing = isKeyguardShowingAndNotOccluded();
        mShutdownActions.showDialog(keyguardShowing, isDeviceProvisioned());
    }

转载自原文链接, 如需删除请联系管理员。

原文链接:android系统自定义关机界面,转载请注明来源!

0