Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.android.systemui.statusbar.preferences;
- import android.app.AlertDialog;
- 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.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.android.internal.R;
- import com.android.internal.app.ShutdownThread;
- import java.util.ArrayList;
- class EosRebootHelper implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
- private final Context mContext;
- private AlertDialog mDialog;
- private MyAdapter mAdapter;
- public EosRebootHelper(Context context) {
- mContext = context;
- IntentFilter filter = new IntentFilter();
- filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
- filter.addAction(Intent.ACTION_SCREEN_OFF);
- context.registerReceiver(mBroadcastReceiver, filter);
- }
- public void showDialog() {
- if (mDialog == null) {
- mDialog = createDialog();
- }
- StatusBarManager statusBarManager = (StatusBarManager) mContext
- .getSystemService("statusbar");
- statusBarManager.collapse();
- mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
- mDialog.show();
- mDialog.getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_DISABLE_EXPAND);
- }
- public boolean isDialogShowing() {
- return ((mDialog == null) ? false : true);
- }
- private AlertDialog createDialog() {
- ArrayList<Action> mLocalItems = new ArrayList<Action>();
- mLocalItems.add(
- (Action) new SinglePressAction(R.drawable.ic_lock_power_off,
- R.string.global_action_power_off) {
- public void onPress() {
- ShutdownThread.shutdown(mContext, true);
- }
- public boolean showDuringKeyguard() {
- return false;
- }
- @Override
- public boolean dismissDialogBeforeAction() {
- return true;
- }
- @Override
- public boolean dismissDialogAfterAction() {
- return true;
- }
- });
- mLocalItems.add(
- (Action) new SinglePressAction(R.drawable.ic_lock_reboot,
- R.string.global_action_reboot) {
- @Override
- public void onPress() {
- ShutdownThread.reboot(mContext, "", false);
- }
- @Override
- public boolean showDuringKeyguard() {
- return true;
- }
- @Override
- public boolean dismissDialogBeforeAction() {
- return true;
- }
- @Override
- public boolean dismissDialogAfterAction() {
- return true;
- }
- });
- mLocalItems.add(
- (Action) new SinglePressAction(R.drawable.ic_lock_reboot_recovery,
- R.string.global_action_reboot_recovery) {
- public void onPress() {
- ShutdownThread.reboot(mContext, "recovery", false);
- }
- @Override
- public boolean showDuringKeyguard() {
- return true;
- }
- @Override
- public boolean dismissDialogBeforeAction() {
- return true;
- }
- @Override
- public boolean dismissDialogAfterAction() {
- return true;
- }
- });
- mLocalItems.add(
- (Action) new SinglePressAction(R.drawable.ic_lock_reboot_bootloader,
- R.string.global_action_reboot_bootloader) {
- @Override
- public boolean showDuringKeyguard() {
- return true;
- }
- @Override
- public boolean dismissDialogBeforeAction() {
- return true;
- }
- @Override
- public boolean dismissDialogAfterAction() {
- return false;
- }
- @Override
- public void onPress() {
- ShutdownThread.reboot(mContext, "bootloader", false);
- }
- });
- mAdapter = new MyAdapter(mLocalItems);
- final AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
- ab.setAdapter(mAdapter, this)
- .setInverseBackgroundForced(true);
- final AlertDialog dialog = ab.create();
- dialog.getListView().setItemsCanFocus(true);
- dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
- dialog.setOnDismissListener(this);
- return dialog;
- }
- public void onDismiss(DialogInterface dialog) {
- }
- public void onClick(DialogInterface dialog, int which) {
- mAdapter.getItem(which).onPress();
- if (mAdapter.getItem(which).dismissDialogAfterAction()) dialog.dismiss();
- }
- private class MyAdapter extends BaseAdapter {
- private ArrayList<Action> mLocalItems;
- public MyAdapter (ArrayList<Action> items){
- super();
- mLocalItems = new ArrayList<Action>();
- mLocalItems.addAll(items);
- }
- public int getCount() {
- int count = 0;
- for (int i = 0; i < mLocalItems.size(); i++) {
- count++;
- }
- return count;
- }
- @Override
- public boolean isEnabled(int position) {
- return getItem(position).isEnabled();
- }
- @Override
- public boolean areAllItemsEnabled() {
- return false;
- }
- public Action getItem(int position) {
- int filteredPos = 0;
- for (int i = 0; i < mLocalItems.size(); i++) {
- final Action action = mLocalItems.get(i);
- if (filteredPos == position) {
- return action;
- }
- filteredPos++;
- }
- throw new IllegalArgumentException("position " + position
- + " out of range of showable actions"
- + ", filtered count=" + getCount());
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- Action action = getItem(position);
- return action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
- }
- }
- private interface Action {
- View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
- void onPress();
- boolean showDuringKeyguard();
- boolean isEnabled();
- boolean dismissDialogBeforeAction();
- boolean dismissDialogAfterAction();
- }
- private static abstract class SinglePressAction implements Action {
- private final int mIconResId;
- private final int mMessageResId;
- protected SinglePressAction(int iconResId, int messageResId) {
- mIconResId = iconResId;
- mMessageResId = messageResId;
- }
- public boolean isEnabled() {
- return true;
- }
- abstract public void onPress();
- public View create(
- Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
- View v = inflater.inflate(R.layout.global_actions_item, parent, false);
- ImageView icon = (ImageView) v.findViewById(R.id.icon);
- TextView messageView = (TextView) v.findViewById(R.id.message);
- v.findViewById(R.id.status).setVisibility(View.GONE);
- icon.setImageDrawable(context.getResources().getDrawable(mIconResId));
- messageView.setText(mMessageResId);
- return v;
- }
- }
- 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)) {
- mHandler.sendEmptyMessage(MESSAGE_DISMISS);
- }
- }
- };
- private static final int MESSAGE_DISMISS = 0;
- private static final int MESSAGE_REFRESH = 1;
- private Handler mHandler = new Handler() {
- public void handleMessage(Message msg) {
- if (msg.what == MESSAGE_DISMISS) {
- if (mDialog != null) {
- mDialog.dismiss();
- }
- } else if (msg.what == MESSAGE_REFRESH) {
- mAdapter.notifyDataSetChanged();
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement