Advertisement
bigrushdog

eosreboothelper

Jan 29th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.26 KB | None | 0 0
  1. package com.android.systemui.statusbar.preferences;
  2.  
  3. import android.app.AlertDialog;
  4. import android.app.StatusBarManager;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.view.WindowManager;
  16. import android.widget.BaseAdapter;
  17. import android.widget.ImageView;
  18. import android.widget.TextView;
  19.  
  20. import com.android.internal.R;
  21. import com.android.internal.app.ShutdownThread;
  22.  
  23. import java.util.ArrayList;
  24.  
  25. class EosRebootHelper implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener  {
  26.  
  27.     private final Context mContext;
  28.  
  29.     private AlertDialog mDialog;
  30.  
  31.     private MyAdapter mAdapter;
  32.  
  33.     public EosRebootHelper(Context context) {
  34.         mContext = context;
  35.  
  36.         IntentFilter filter = new IntentFilter();
  37.         filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
  38.         filter.addAction(Intent.ACTION_SCREEN_OFF);
  39.         context.registerReceiver(mBroadcastReceiver, filter);
  40.     }
  41.  
  42.     public void showDialog() {
  43.         if (mDialog == null) {
  44.             mDialog = createDialog();
  45.         }
  46.         StatusBarManager statusBarManager = (StatusBarManager) mContext
  47.                 .getSystemService("statusbar");
  48.         statusBarManager.collapse();
  49.         mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
  50.         mDialog.show();
  51.         mDialog.getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_DISABLE_EXPAND);
  52.     }
  53.    
  54.     public boolean isDialogShowing() {
  55.         return ((mDialog == null) ? false : true);
  56.     }
  57.  
  58.     private AlertDialog createDialog() {
  59.          ArrayList<Action> mLocalItems = new ArrayList<Action>();
  60.         mLocalItems.add(
  61.                 (Action) new SinglePressAction(R.drawable.ic_lock_power_off,
  62.                         R.string.global_action_power_off) {
  63.  
  64.                     public void onPress() {
  65.                         ShutdownThread.shutdown(mContext, true);
  66.                     }
  67.  
  68.                     public boolean showDuringKeyguard() {
  69.                         return false;
  70.                     }
  71.  
  72.                     @Override
  73.                     public boolean dismissDialogBeforeAction() {
  74.                         return true;
  75.                     }
  76.  
  77.                     @Override
  78.                     public boolean dismissDialogAfterAction() {
  79.                         return true;
  80.                     }
  81.                 });
  82.         mLocalItems.add(
  83.                 (Action) new SinglePressAction(R.drawable.ic_lock_reboot,
  84.                     R.string.global_action_reboot) {
  85.             @Override
  86.             public void onPress() {
  87.                  ShutdownThread.reboot(mContext, "", false);
  88.             }
  89.             @Override
  90.             public boolean showDuringKeyguard() {
  91.                 return true;
  92.             }
  93.  
  94.             @Override
  95.             public boolean dismissDialogBeforeAction() {
  96.                 return true;
  97.             }
  98.  
  99.             @Override
  100.             public boolean dismissDialogAfterAction() {
  101.                 return true;
  102.             }
  103.         });
  104.         mLocalItems.add(
  105.                 (Action) new SinglePressAction(R.drawable.ic_lock_reboot_recovery,
  106.                 R.string.global_action_reboot_recovery) {
  107.             public void onPress() {
  108.                 ShutdownThread.reboot(mContext, "recovery", false);
  109.             }
  110.             @Override
  111.             public boolean showDuringKeyguard() {
  112.                 return true;
  113.             }
  114.             @Override
  115.             public boolean dismissDialogBeforeAction() {
  116.                 return true;
  117.             }
  118.             @Override
  119.             public boolean dismissDialogAfterAction() {
  120.                 return true;
  121.             }
  122.         });
  123.         mLocalItems.add(
  124.                 (Action) new SinglePressAction(R.drawable.ic_lock_reboot_bootloader,
  125.                 R.string.global_action_reboot_bootloader) {
  126.             @Override
  127.             public boolean showDuringKeyguard() {
  128.                 return true;
  129.             }
  130.             @Override
  131.             public boolean dismissDialogBeforeAction() {
  132.                 return true;
  133.             }
  134.             @Override
  135.             public boolean dismissDialogAfterAction() {
  136.                 return false;
  137.             }
  138.             @Override
  139.             public void onPress() {
  140.                 ShutdownThread.reboot(mContext, "bootloader", false);
  141.             }
  142.         });
  143.  
  144.         mAdapter = new MyAdapter(mLocalItems);
  145.  
  146.         final AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
  147.  
  148.         ab.setAdapter(mAdapter, this)
  149.         .setInverseBackgroundForced(true);
  150.  
  151.         final AlertDialog dialog = ab.create();
  152.         dialog.getListView().setItemsCanFocus(true);
  153.         dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
  154.  
  155.         dialog.setOnDismissListener(this);
  156.  
  157.         return dialog;
  158.         }
  159.  
  160.     public void onDismiss(DialogInterface dialog) {
  161.     }
  162.  
  163.     public void onClick(DialogInterface dialog, int which) {
  164.         mAdapter.getItem(which).onPress();
  165.         if (mAdapter.getItem(which).dismissDialogAfterAction()) dialog.dismiss();
  166.     }
  167.  
  168.     private class MyAdapter extends BaseAdapter {
  169.         private ArrayList<Action> mLocalItems;
  170.        
  171.         public MyAdapter (ArrayList<Action> items){
  172.             super();
  173.             mLocalItems = new ArrayList<Action>();
  174.             mLocalItems.addAll(items);
  175.         }
  176.        
  177.         public int getCount() {
  178.             int count = 0;
  179.  
  180.             for (int i = 0; i < mLocalItems.size(); i++) {
  181.                 count++;
  182.             }
  183.             return count;
  184.         }
  185.  
  186.         @Override
  187.         public boolean isEnabled(int position) {
  188.             return getItem(position).isEnabled();
  189.         }
  190.  
  191.         @Override
  192.         public boolean areAllItemsEnabled() {
  193.             return false;
  194.         }
  195.  
  196.         public Action getItem(int position) {
  197.  
  198.             int filteredPos = 0;
  199.             for (int i = 0; i < mLocalItems.size(); i++) {
  200.                 final Action action = mLocalItems.get(i);
  201.                 if (filteredPos == position) {
  202.                     return action;
  203.                 }
  204.                 filteredPos++;
  205.             }
  206.  
  207.             throw new IllegalArgumentException("position " + position
  208.                     + " out of range of showable actions"
  209.                     + ", filtered count=" + getCount());
  210.         }
  211.  
  212.         public long getItemId(int position) {
  213.             return position;
  214.         }
  215.  
  216.         public View getView(int position, View convertView, ViewGroup parent) {
  217.             Action action = getItem(position);
  218.             return action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
  219.         }
  220.     }
  221.  
  222.     private interface Action {
  223.         View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
  224.  
  225.         void onPress();
  226.  
  227.         boolean showDuringKeyguard();
  228.  
  229.         boolean isEnabled();
  230.        
  231.         boolean dismissDialogBeforeAction();
  232.        
  233.         boolean dismissDialogAfterAction();
  234.     }
  235.  
  236.     private static abstract class SinglePressAction implements Action {
  237.         private final int mIconResId;
  238.         private final int mMessageResId;
  239.  
  240.         protected SinglePressAction(int iconResId, int messageResId) {
  241.             mIconResId = iconResId;
  242.             mMessageResId = messageResId;
  243.         }
  244.  
  245.         public boolean isEnabled() {
  246.             return true;
  247.         }
  248.  
  249.         abstract public void onPress();
  250.  
  251.         public View create(
  252.                 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
  253.             View v = inflater.inflate(R.layout.global_actions_item, parent, false);
  254.  
  255.             ImageView icon = (ImageView) v.findViewById(R.id.icon);
  256.             TextView messageView = (TextView) v.findViewById(R.id.message);
  257.  
  258.             v.findViewById(R.id.status).setVisibility(View.GONE);
  259.  
  260.             icon.setImageDrawable(context.getResources().getDrawable(mIconResId));
  261.             messageView.setText(mMessageResId);
  262.  
  263.             return v;
  264.         }
  265.     }
  266.    
  267.     private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
  268.         public void onReceive(Context context, Intent intent) {
  269.             String action = intent.getAction();
  270.             if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
  271.                     || Intent.ACTION_SCREEN_OFF.equals(action)) {
  272.                     mHandler.sendEmptyMessage(MESSAGE_DISMISS);
  273.             }
  274.         }
  275.     };
  276.    
  277.     private static final int MESSAGE_DISMISS = 0;
  278.     private static final int MESSAGE_REFRESH = 1;
  279.  
  280.     private Handler mHandler = new Handler() {
  281.         public void handleMessage(Message msg) {
  282.             if (msg.what == MESSAGE_DISMISS) {
  283.                 if (mDialog != null) {
  284.                     mDialog.dismiss();
  285.                 }
  286.             } else if (msg.what == MESSAGE_REFRESH) {
  287.                 mAdapter.notifyDataSetChanged();
  288.             }
  289.         }
  290.     };
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement