Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b7e787fd authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Switch to new improves shutdown class that is now in framework.

parent 91781856
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.internal.R;
import com.android.internal.app.ShutdownThread;
import com.google.android.collect.Lists;

import java.util.ArrayList;
@@ -176,7 +177,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac

                    public void onPress() {
                        // shutdown by making sure radio and power are handled accordingly.
                        ShutdownThread.shutdownAfterDisablingRadio(mContext, true);
                        ShutdownThread.shutdown(mContext, true);
                    }

                    public boolean showDuringKeyguard() {
+3 −1
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ import static android.provider.Telephony.Intents.EXTRA_SHOW_PLMN;
import static android.provider.Telephony.Intents.EXTRA_SHOW_SPN;
import static android.provider.Telephony.Intents.EXTRA_SPN;
import static android.provider.Telephony.Intents.SPN_STRINGS_UPDATED_ACTION;

import com.android.internal.app.ShutdownThread;
import com.android.internal.telephony.IccCard;
import com.android.internal.telephony.TelephonyIntents;
import android.util.Log;
@@ -298,7 +300,7 @@ public class KeyguardUpdateMonitor {
        if (batteryLevel == 0 &&
                pluggedInStatus != BATTERY_STATUS_CHARGING &&
                pluggedInStatus != BATTERY_STATUS_UNKNOWN) {
            ShutdownThread.shutdownAfterDisablingRadio(mContext, false);
            ShutdownThread.shutdown(mContext, false);
        }
    }

+3 −3
Original line number Diff line number Diff line
@@ -23,11 +23,11 @@ import android.app.StatusBarManager;
import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.IServiceManager;
import android.os.LocalPowerManager;
import android.os.ServiceManager;
import android.os.ServiceManagerNative;
import android.os.SystemClock;

import com.android.internal.app.ShutdownThread;
import com.android.internal.telephony.ITelephony;
import android.view.KeyEvent;
import android.util.Log;
@@ -123,7 +123,7 @@ public class PowerDialog extends Dialog implements OnClickListener,
        this.dismiss();
        if (v == mPower) {
            // shutdown by making sure radio and power are handled accordingly.
            ShutdownThread.shutdownAfterDisablingRadio(getContext(), true);
            ShutdownThread.shutdown(getContext(), true);
        } else if (v == mRadioPower) {
            try {
                ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
+0 −173
Original line number Diff line number Diff line
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 
package com.android.internal.policy.impl;

import android.app.ProgressDialog;
import android.app.AlertDialog;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.IBluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.os.RemoteException;
import android.os.Power;
import android.os.ServiceManager;
import android.os.SystemClock;
import com.android.internal.telephony.ITelephony;
import android.util.Log;
import android.view.WindowManager;

 
final class ShutdownThread extends Thread {
    // constants
    private static final String TAG = "ShutdownThread";
    private static final int MAX_NUM_PHONE_STATE_READS = 16;
    private static final int PHONE_STATE_POLL_SLEEP_MSEC = 500;
    private static final ITelephony sPhone =
        ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
    private static final IBluetoothDevice sBluetooth =
        IBluetoothDevice.Stub.asInterface(ServiceManager.getService(Context.BLUETOOTH_SERVICE));


    // state tracking
    private static Object sIsStartedGuard = new Object();
    private static boolean sIsStarted = false;
    
    // static instance of this thread
    private static final ShutdownThread sInstance = new ShutdownThread();
    
    private ShutdownThread() {
    }
 
    /** 
     * request a shutdown. 
     * 
     * @param context Context used to display the shutdown progress dialog.
     */
    public static void shutdownAfterDisablingRadio(final Context context, boolean confirm){
        // ensure that only one thread is trying to power down.
        // any additional calls are just returned
        synchronized (sIsStartedGuard){
            if (sIsStarted) {
                Log.d(TAG, "Request to shutdown already running, returning.");
                return;
            }
        }

        Log.d(TAG, "Notifying thread to start radio shutdown");

        if (confirm) {
            final AlertDialog dialog = new AlertDialog.Builder(context)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle(com.android.internal.R.string.power_off)
                    .setMessage(com.android.internal.R.string.shutdown_confirm)
                    .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            beginShutdownSequence(context);
                        }
                    })
                    .setNegativeButton(com.android.internal.R.string.no, null)
                    .create();
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
            dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
            dialog.show();
        } else {
            beginShutdownSequence(context);
        }
    }

    private static void beginShutdownSequence(Context context) {
        synchronized (sIsStartedGuard) {
            sIsStarted = true;
        }

        // throw up an indeterminate system dialog to indicate radio is
        // shutting down.
        ProgressDialog pd = new ProgressDialog(context);
        pd.setTitle(context.getText(com.android.internal.R.string.power_off));
        pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));
        pd.setIndeterminate(true);
        pd.setCancelable(false);
        pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
        pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

        pd.show();

        // start the thread that initiates shutdown
        sInstance.start();
    }

    /**
     * Makes sure we handle the shutdown gracefully.
     * Shuts off power regardless of radio and bluetooth state if the alloted time has passed.
     */
    public void run() {
        boolean bluetoothOff;
        boolean radioOff;

        try {
            bluetoothOff = sBluetooth == null ||
                           sBluetooth.getBluetoothState() == BluetoothDevice.BLUETOOTH_STATE_OFF;
            if (!bluetoothOff) {
                sBluetooth.disable(false);  // disable but don't persist new state
            }
        } catch (RemoteException ex) {
            Log.e(TAG, "RemoteException during bluetooth shutdown", ex);
            bluetoothOff = true;
        }

        try {
            radioOff = sPhone == null || !sPhone.isRadioOn();
            if (!radioOff) {
                sPhone.setRadio(false);
            }
        } catch (RemoteException ex) {
            Log.e(TAG, "RemoteException during radio shutdown", ex);
            radioOff = true;
        }

        // Wait a max of 32 seconds for clean shutdown
        for (int i = 0; i < MAX_NUM_PHONE_STATE_READS; i++) {
            if (!bluetoothOff) {
                try {
                    bluetoothOff =
                            sBluetooth.getBluetoothState() == BluetoothDevice.BLUETOOTH_STATE_OFF;
                } catch (RemoteException ex) {
                    Log.e(TAG, "RemoteException during bluetooth shutdown", ex);
                    bluetoothOff = true;
                }
            }
            if (!radioOff) {
                try {
                    radioOff = !sPhone.isRadioOn();
                } catch (RemoteException ex) {
                    Log.e(TAG, "RemoteException during radio shutdown", ex);
                    radioOff = true;
                }
            }
            if (radioOff && bluetoothOff) {
                Log.d(TAG, "Radio and Bluetooth shutdown complete.");
                break;
            }
            SystemClock.sleep(PHONE_STATE_POLL_SLEEP_MSEC);
        }

        //shutdown power
        Log.d(TAG, "Shutting down power.");
        Power.shutdown();
    }
}