OSDN Git Service

Implement poweroff function in Settings menu
[android-x86/packages-apps-Settings.git] / src / com / android / settings / PowerOff.java
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.settings;
18
19 import android.app.Activity;
20 import android.app.ActivityManager;
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.util.Slog;
24 import android.content.BroadcastReceiver;
25 import android.content.DialogInterface;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.os.Build;
29 import android.os.Bundle;
30 import android.preference.Preference;
31 import android.preference.PreferenceGroup;
32 import android.preference.PreferenceScreen;
33 import android.provider.Settings;
34 import android.util.Log;
35 import android.view.WindowManager;
36 import android.content.Context;
37
38 import java.io.BufferedReader;
39 import java.io.FileReader;
40 import java.io.IOException;
41 import java.util.List;
42 import android.os.Handler;
43
44 public class PowerOff extends SettingsPreferenceFragment{
45         private Context mContext;
46         public void onCreate(Bundle icicle) {
47                 super.onCreate(icicle);
48                 }
49         public void onStart()   {
50                 super.onStart();
51                 beginshutdown();
52                 }
53
54         public void beginshutdown()
55             {
56                 final CloseDialogReceiver closer = new CloseDialogReceiver(getActivity());
57                 final AlertDialog dialog = new AlertDialog.Builder(getActivity())
58                 .setIconAttribute(android.R.attr.alertDialogIcon)
59                 .setTitle(com.android.internal.R.string.power_off)
60                 .setMessage(com.android.internal.R.string.shutdown_confirm_question)
61                 .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
62                     public void onClick(DialogInterface dialog, int which) {
63                         Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
64                         shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
65                         shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
66                         startActivity(shutdown);
67                     }
68                 })
69                 .setNegativeButton(com.android.internal.R.string.no, new DialogInterface.OnClickListener() {
70                     public void onClick(DialogInterface dialog, int which) {
71                         Intent close = new Intent(Intent.ACTION_MAIN);
72                         close.addCategory(Intent.CATEGORY_HOME);
73                         close.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
74                         startActivity(close);
75                         finish();
76             }
77                 })
78                 .create();
79                 closer.dialog = dialog;
80                 dialog.setOnDismissListener(closer);
81                 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
82                 if (!getActivity().getResources().getBoolean(
83                     com.android.internal.R.bool.config_sf_slowBlur)) {
84                 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
85                 }
86                 dialog.show();
87
88         }
89         private static class CloseDialogReceiver extends BroadcastReceiver
90             implements DialogInterface.OnDismissListener {
91                 private Context mContext;
92                 public Dialog dialog;
93
94                 CloseDialogReceiver(Context context) {
95                                 mContext = context;
96                                 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
97                                 context.registerReceiver(this, filter);
98                 }
99
100                 @Override
101                 public void onReceive(Context context, Intent intent) {
102                         dialog.cancel();
103                 }
104
105                 public void onDismiss(DialogInterface unused) {
106                         mContext.unregisterReceiver(this);
107                 }
108             }
109         public void onResume()  {
110                 super.onResume();
111                 }
112         public void onPause()   {
113                 super.onPause();
114                 }
115         public void onStop()    {
116                 super.onStop();
117                 }
118         public void onDestroy() {
119                 super.onDestroy();
120                 }
121         }