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

Commit 00c11558 authored by Roman Birg's avatar Roman Birg
Browse files

Add "Turn WiFi off" action to "Open networks" notification



Forward ported to kitkat

Squashed:
add "Turn Wi-Fi off" action to the "open networks available"
notification
add missing xhdpi turn wifi off image
WiFi open network notification: fix potential crash

Change-Id: Id1fd4f50755c5172cdc557fc27e2298290edecef
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent 968d9674
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -208,6 +208,9 @@
    <string name="app_ops_write_mms">write an MMS message</string>
    <string name="app_ops_write_sms">write an SMS message</string>

    <!-- WiFi turn off notification -->
    <string name="notify_turn_wifi_off_title">Turn Wi-Fi off</string>

    <string name="privacy_guard_dialog_title">Privacy Guard</string>
    <string name="privacy_guard_dialog_summary"><xliff:g id="app">%1$s</xliff:g> would like to <xliff:g id="op">%2$s</xliff:g>.</string>

+3 −0
Original line number Diff line number Diff line
@@ -2005,6 +2005,9 @@
  <!-- SMS RIL_REQUEST_SEND_SMS_EXPECT_MORE -->
  <java-symbol type="bool" name="config_smsUseExpectMore" />

  <!-- WiFi turn off notification -->
  <java-symbol type="string" name="notify_turn_wifi_off_title" />

  <!-- Label for the Android system components when they are shown to the user. -->
  <java-symbol type="string" name="android_system_label" />

+53 −14
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.wifi;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
@@ -29,6 +30,7 @@ import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiStateMachine;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.os.UserHandle;
@@ -46,6 +48,15 @@ final class WifiNotificationController {
     */
    private static final int ICON_NETWORKS_AVAILABLE =
            com.android.internal.R.drawable.stat_notify_wifi_in_range;
    /**
    * Listen for this intent when the notification is showing
    */
    private static final String ACTION_TURN_WIFI_OFF = "com.android.server.WifiService.ACTION_TURN_WIFI_OFF";
    /**
    * Turns wifi off when run (IntentFilter responsible for specifying the action
    * ACTION_TURN_WIFI_OFF)
    */
    private static NotificationBroadcastReciever mNotificationBroadcastReceiver = null;
    /**
     * When a notification is shown, we wait this amount before possibly showing it again.
     */
@@ -92,6 +103,7 @@ final class WifiNotificationController {
    private final WifiStateMachine mWifiStateMachine;
    private NetworkInfo mNetworkInfo;
    private volatile int mWifiState;
    private WifiManager wifiManager;

    WifiNotificationController(Context context, WifiStateMachine wsm) {
        mContext = context;
@@ -227,36 +239,63 @@ final class WifiNotificationController {
                return;
            }

            if (mNotification == null) {
                // Cache the Notification object.
                mNotification = new Notification();
                mNotification.when = 0;
                mNotification.icon = ICON_NETWORKS_AVAILABLE;
                mNotification.flags = Notification.FLAG_AUTO_CANCEL;
                mNotification.contentIntent = TaskStackBuilder.create(mContext)
                        .addNextIntentWithParentStack(
                                new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK))
                        .getPendingIntent(0, 0, null, UserHandle.CURRENT);
            }

            CharSequence title = mContext.getResources().getQuantityText(
                    com.android.internal.R.plurals.wifi_available, numNetworks);
            CharSequence details = mContext.getResources().getQuantityText(
                    com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
            mNotification.tickerText = title;
            mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
            mNotification = new Notification.Builder(mContext)
                    .setWhen(0)
                    .setSmallIcon(ICON_NETWORKS_AVAILABLE)
                    .setAutoCancel(true)
                    .setContentIntent(TaskStackBuilder.create(mContext)
                        .addNextIntentWithParentStack(
                                new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK))
                        .getPendingIntent(0, 0, null, UserHandle.CURRENT))
                    .addAction(0, mContext.getText(
                            com.android.internal.R.string.notify_turn_wifi_off_title),
                            PendingIntent.getBroadcast(mContext, 0,
                                    new Intent(ACTION_TURN_WIFI_OFF).setPackage(
                                            mContext.getPackageName()),
                                            PendingIntent.FLAG_ONE_SHOT))
                    .setTicker(title)
                    .setContentTitle(title)
                    .setContentText(details)
                    .build();

            mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;

            notificationManager.notifyAsUser(null, ICON_NETWORKS_AVAILABLE, mNotification,
                    UserHandle.ALL);

            if (mNotificationBroadcastReceiver == null) {
                mNotificationBroadcastReceiver = new NotificationBroadcastReciever();
                IntentFilter filter = new IntentFilter(ACTION_TURN_WIFI_OFF);
                mContext.registerReceiver(mNotificationBroadcastReceiver, filter);
            }
        } else {
            if  (mNotificationBroadcastReceiver != null) {
                mContext.unregisterReceiver(mNotificationBroadcastReceiver);
                mNotificationBroadcastReceiver = null;
            }
            notificationManager.cancelAsUser(null, ICON_NETWORKS_AVAILABLE, UserHandle.ALL);
        }

        mNotificationShown = visible;
    }

    private class NotificationBroadcastReciever extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            AsyncTask.execute(new Runnable() {
                public void run() {
                    WifiManager wifiManager = (WifiManager)
                                mContext.getSystemService(Context.WIFI_SERVICE);
                    wifiManager.setWifiEnabled(false);
                }
            });
        }
    }

    void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        pw.println("mNotificationEnabled " + mNotificationEnabled);
        pw.println("mNotificationRepeatTime " + mNotificationRepeatTime);