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

Commit e7085e90 authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Update framework tethering UI

Getting rid of notification dialog - maybe the entire TetherActivity.
Also getting rid of toasts - to ephemeral.
parent 50490adf
Loading
Loading
Loading
Loading
+0 −7
Original line number Diff line number Diff line
@@ -3084,13 +3084,6 @@ public final class Settings {
         */
        public static final String MOUNT_UMS_NOTIFY_ENABLED = "mount_ums_notify_enabled";

        /**
         * Whether or not a notification is displayed when a Tetherable interface is detected.
         * (0 = false, 1 = true)
         * @hide
         */
        public static final String TETHER_NOTIFY = "tether_notify";

        /**
         * If nonzero, ANRs in invisible background processes bring up a dialog.
         * Otherwise, the process will be silently killed.
+0 −179
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 Google Inc.
 *
 * 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.app;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.widget.Toast;
import android.util.Log;

/**
 * This activity is shown to the user in two cases: when a connection is possible via
 * a usb tether and when any type of tether is connected.  In the connecting case
 * It allows them to start a USB tether.  In the Tethered/disconnecting case it
 * will disconnect all tethers.
 */
public class TetherActivity extends AlertActivity implements
        DialogInterface.OnClickListener {

    private static final int POSITIVE_BUTTON = AlertDialog.BUTTON1;

    // count of the number of tethered connections at activity create time.
    private int mTethered;

    /* Used to detect when the USB cable is unplugged, so we can call finish() */
    private BroadcastReceiver mTetherReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == ConnectivityManager.ACTION_TETHER_STATE_CHANGED) {
                handleTetherStateChanged(intent);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // determine if we advertise tethering or untethering
        ConnectivityManager cm =
                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

        mTethered = cm.getTetheredIfaces().length;
        int tetherable = cm.getTetherableIfaces().length;
        if ((mTethered == 0) && (tetherable == 0)) {
            finish();
            return;
        }

        // Set up the dialog
        // if we have a tethered connection we put up a "Do you want to Disconect" dialog
        // otherwise we must have a tetherable interface (else we'd return above)
        // and so we want to put up the "do you want to connect" dialog
        if (mTethered == 0) {
            mAlertParams.mIconId = com.android.internal.R.drawable.ic_dialog_usb;
            mAlertParams.mTitle = getString(com.android.internal.R.string.tether_title);
            mAlertParams.mMessage = getString(com.android.internal.R.string.tether_message);
            mAlertParams.mPositiveButtonText =
                    getString(com.android.internal.R.string.tether_button);
            mAlertParams.mPositiveButtonListener = this;
            mAlertParams.mNegativeButtonText =
                    getString(com.android.internal.R.string.tether_button_cancel);
            mAlertParams.mNegativeButtonListener = this;
        } else {
            mAlertParams.mIconId = com.android.internal.R.drawable.ic_dialog_usb;
            mAlertParams.mTitle = getString(com.android.internal.R.string.tether_stop_title);
            mAlertParams.mMessage = getString(com.android.internal.R.string.tether_stop_message);
            mAlertParams.mPositiveButtonText =
                    getString(com.android.internal.R.string.tether_stop_button);
            mAlertParams.mPositiveButtonListener = this;
            mAlertParams.mNegativeButtonText =
                    getString(com.android.internal.R.string.tether_stop_button_cancel);
            mAlertParams.mNegativeButtonListener = this;
        }
        setupAlert();
    }

    @Override
    protected void onResume() {
        super.onResume();

        registerReceiver(mTetherReceiver, new IntentFilter(
                ConnectivityManager.ACTION_TETHER_STATE_CHANGED));
    }

    @Override
    protected void onPause() {
        super.onPause();

        unregisterReceiver(mTetherReceiver);
    }

    /**
     * {@inheritDoc}
     */
    public void onClick(DialogInterface dialog, int which) {
        int error = ConnectivityManager.TETHER_ERROR_NO_ERROR;

        if (which == POSITIVE_BUTTON) {
            ConnectivityManager cm =
                    (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            // start/stop tethering
            String[] tethered = cm.getTetheredIfaces();

            if (tethered.length == 0) {
                String[] tetherable = cm.getTetherableIfaces();
                String[] usbRegexs = cm.getTetherableUsbRegexs();
                for (String t : tetherable) {
                    for (String r : usbRegexs) {
                        if (t.matches(r)) {
                            error = cm.tether(t);
                            break;
                        }
                    }
                }
                showTetheringError(error);
            } else {
                for (String t : tethered) {
                    error = cm.untether(t);
                }
                showUnTetheringError(error);
            }
        }
        // No matter what, finish the activity
        finish();
    }

    private void handleTetherStateChanged(Intent intent) {
        // determine if we advertise tethering or untethering
        ConnectivityManager cm =
                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        if (mTethered != cm.getTetheredIfaces().length) {
            finish();
        }
    }

    private void showTetheringError(int error) {
        switch(error) {
        case ConnectivityManager.TETHER_ERROR_NO_ERROR:
            return;
        default:
            Toast.makeText(this, com.android.internal.R.string.tether_error_message,
                    Toast.LENGTH_LONG).show();
        }
    }

    private void showUnTetheringError(int error) {
        switch(error) {
        case ConnectivityManager.TETHER_ERROR_NO_ERROR:
            return;
        default:
            Toast.makeText(this, com.android.internal.R.string.tether_stop_error_message,
                    Toast.LENGTH_LONG).show();
        }
    }
}
+0 −4
Original line number Diff line number Diff line
@@ -1271,10 +1271,6 @@
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name="com.android.internal.app.TetherActivity"
                android:theme="@style/Theme.Dialog.Alert"
                android:excludeFromRecents="true">
        </activity>
        <activity android:name="com.android.server.status.UsbStorageActivity"
                android:excludeFromRecents="true">
        </activity>
+1 −39
Original line number Diff line number Diff line
@@ -1101,7 +1101,7 @@
      the state of network connectivity.</string>

    <!-- Title of an application permission, listed so the user can choose whether they want to allow the application to do this. -->
    <string name="permlab_changeTetherState">change tethered connectivity</string>
    <string name="permlab_changeTetherState">Change tethered connectivity</string>
    <!-- Description of an application permission, listed so the user can choose whether they want to allow the applicaiton to do this. -->
    <string name="permdesc_changeTetherState">Allows an application to change
      the state of tethered network connectivity.</string>
@@ -2246,44 +2246,6 @@
    -->
    <string name="description_star">favorite</string>


    <!-- Strings for Tethering dialogs -->
    <!-- This is the label for the activity, and should never be visible to the user. -->
    <!-- See TETHERING.  TETHERING_DIALOG:  After the user selects the notification, a dialog is shown asking if he wants to Tether.  This is the title. -->
    <string name="tether_title">USB tethering available</string>
    <!-- See TETHER.    This is the message. -->
    <string name="tether_message">Select \"Tether\" if you want to share your phone\'s data connection with your computer.</string>
    <!-- See TETHER.    This is the button text to Tether the computer with the phone. -->
    <string name="tether_button">Tether</string>
    <!-- See TETHER.   This is the button text to ignore the plugging in of the phone.. -->
    <string name="tether_button_cancel">Cancel</string>

    <!-- See TETHER.  If there was a recoverable error, this is the text. -->
    <string name="tether_error_message">We\'ve encountered a problem turning on Tethering.  Please try again.</string>

    <!-- TETHER: When the user connects the phone to a computer, we show a notification asking if he wants to share his cellular network connection.  This is the title -->
    <string name="tether_available_notification_title">USB tethering available</string>
    <!-- See USB_STORAGE. This is the message. -->
    <string name="tether_available_notification_message">Select to tether your computer to your phone.</string>
    <!-- TETHER_STOP: While TETHER is enabled, we show a notification dialog asking if he wants to stop. This is the title -->
    <string name="tether_stop_notification_title">Untether</string>
    <!-- See TETHER. This is the message. -->
    <string name="tether_stop_notification_message">Select to untether your computer.</string>

    <!-- TETHER stop dialog strings -->
    <!-- This is the label for the activity, and should never be visible to the user. -->
    <!-- See TETHER_STOP.  TETHER_STOP_DIALOG:  After the user selects the notification, a dialog is shown asking if he wants to stop tethering.  This is the title. -->
    <string name="tether_stop_title">Disconnect tethering</string>
    <!-- See TETHER_STOP.    This is the message. -->
    <string name="tether_stop_message">You have been sharing your phone\'s cellular data connection with your computer. Select \"Disconnect\" to disconnect USB tethering.</string>
    <!-- See TETHER_STOP.    This is the button text to disconnect tethering. -->
    <string name="tether_stop_button">Disconnect</string>
    <!-- See TETHER_STOP.   This is the button text to cancel disconnecting the tether. -->
    <string name="tether_stop_button_cancel">Cancel</string>

    <!-- See TETHER_STOP.  If there was an error disconnect, this is the text. -->
    <string name="tether_stop_error_message">We\'ve encountered a problem turning off Tethering. Please try again.</string>

    <!-- Strings for car mode notification -->
    <!-- Shown when car mode is enabled -->
    <string name="car_mode_disable_notification_title">Car mode enabled</string>
+0 −142
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ import android.os.RemoteException;
import android.os.ServiceManager;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import com.android.internal.telephony.Phone;
import com.android.internal.util.HierarchicalState;
@@ -66,7 +65,6 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
    private Context mContext;
    private final String TAG = "Tethering";

    private boolean mPlaySounds = false;
    private boolean mBooted = false;
    //used to remember if we got connected before boot finished
    private boolean mDeferedUsbConnection = false;
@@ -78,8 +76,6 @@ public class Tethering extends INetworkManagementEventObserver.Stub {

    private HashMap<String, TetherInterfaceSM> mIfaces;

    private ArrayList<String> mActiveTtys;

    private BroadcastReceiver mStateReceiver;

    private static final String USB_NEAR_IFACE_ADDR      = "169.254.2.1";
@@ -112,7 +108,6 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
        }

        mIfaces = new HashMap<String, TetherInterfaceSM>();
        mActiveTtys = new ArrayList<String>();

        mTetherMasterSM = new TetherMasterSM("TetherMaster");
        mTetherMasterSM.start();
@@ -323,142 +318,6 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
        mContext.sendStickyBroadcast(broadcast);
        Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
                activeList.size() + ", " + erroredList.size());
        // check if we need to send a USB notification
        // Check if the user wants to be bothered
        boolean tellUser = (Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.TETHER_NOTIFY, 0) == 1);
        for (Object o : activeList) {
            String s = (String)o;
            for (Object regexObject : mTetherableUsbRegexs) {
                if (s.matches((String)regexObject)) {
                    showTetheredNotification();
                    return;
                }
            }
        }
        if (tellUser) {
            for (Object o : availableList) {
                String s = (String)o;
                for (String match : mTetherableUsbRegexs) {
                    if (s.matches(match)) {
                        showTetherAvailableNotification();
                        return;
                    }
                }
            }
        }
        clearNotification();
    }

    private void showTetherAvailableNotification() {
        NotificationManager notificationManager = (NotificationManager)mContext.
                getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            return;
        }
        Intent intent = new Intent();
        intent.setClass(mContext, com.android.internal.app.TetherActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);

        Resources r = Resources.getSystem();
        CharSequence title = r.getText(com.android.internal.R.string.
                tether_available_notification_title);
        CharSequence message = r.getText(com.android.internal.R.string.
                tether_available_notification_message);

        if(mTetheringNotification == null) {
            mTetheringNotification = new Notification();
            mTetheringNotification.when = 0;
        }
        mTetheringNotification.icon = com.android.internal.R.drawable.stat_sys_tether_usb;

        boolean playSounds = false;
        //playSounds = SystemProperties.get("persist.service.mount.playsnd", "1").equals("1");
        if (playSounds) {
            mTetheringNotification.defaults |= Notification.DEFAULT_SOUND;
        } else {
            mTetheringNotification.defaults &= ~Notification.DEFAULT_SOUND;
        }

        mTetheringNotification.flags = Notification.FLAG_ONGOING_EVENT;
        mTetheringNotification.tickerText = title;
        mTetheringNotification.setLatestEventInfo(mContext, title, message, pi);

        notificationManager.notify(mTetheringNotification.icon, mTetheringNotification);

    }

    private void showTetheredNotification() {
        NotificationManager notificationManager = (NotificationManager)mContext.
                getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setClass(mContext, com.android.internal.app.TetherActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);

        Resources r = Resources.getSystem();
        CharSequence title = r.getText(com.android.internal.R.string.
                tether_stop_notification_title);
        CharSequence message = r.getText(com.android.internal.R.string.
                tether_stop_notification_message);

        if(mTetheringNotification == null) {
            mTetheringNotification = new Notification();
            mTetheringNotification.when = 0;
        }
        mTetheringNotification.icon = com.android.internal.R.drawable.stat_sys_tether_usb;

        boolean playSounds = false;
        //playSounds = SystemProperties.get("persist.service.mount.playsnd", "1").equals("1");
        if (playSounds) {
            mTetheringNotification.defaults |= Notification.DEFAULT_SOUND;
        } else {
            mTetheringNotification.defaults &= ~Notification.DEFAULT_SOUND;
        }

        mTetheringNotification.flags = Notification.FLAG_ONGOING_EVENT;
        mTetheringNotification.tickerText = title;
        mTetheringNotification.setLatestEventInfo(mContext, title, message, pi);

        notificationManager.notify(mTetheringNotification.icon, mTetheringNotification);
    }

    private void clearNotification() {
        NotificationManager notificationManager = (NotificationManager)mContext.
                getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null && mTetheringNotification != null) {
            notificationManager.cancel(mTetheringNotification.icon);
            mTetheringNotification = null;
        }
    }

    private void showErrorToast(int error) {
        int num;
        switch(error) {
        case ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR:
        case ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR:
        case ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR:
        case ConnectivityManager.TETHER_ERROR_MASTER_ERROR:
            num = com.android.internal.R.string.tether_error_message;
            break;
        case ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR:
        case ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR:
            num = com.android.internal.R.string.tether_stop_error_message;
            break;
        default:
            // do nothing
            return;
        }
        String text = mContext.getResources().getString(num) + " - EC" + error;
        Log.e(TAG, text);
        Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
    }

    private class StateReceiver extends BroadcastReceiver {
@@ -748,7 +607,6 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
                    // further error..
                    Tethering.this.configureUsbIface(false);
                }
                Tethering.this.showErrorToast(error);
            }
        }