UsbDeviceManager.java revision 78a9687fc2eab03b9d764cd27eaa1d56c66960be
146d0adf8256a42416584765625852b6e48497c90Mike Lockwood/*
246d0adf8256a42416584765625852b6e48497c90Mike Lockwood * Copyright (C) 2011 The Android Open Source Project
346d0adf8256a42416584765625852b6e48497c90Mike Lockwood *
446d0adf8256a42416584765625852b6e48497c90Mike Lockwood * Licensed under the Apache License, Version 2.0 (the "License");
546d0adf8256a42416584765625852b6e48497c90Mike Lockwood * you may not use this file except in compliance with the License.
646d0adf8256a42416584765625852b6e48497c90Mike Lockwood * You may obtain a copy of the License at
746d0adf8256a42416584765625852b6e48497c90Mike Lockwood *
846d0adf8256a42416584765625852b6e48497c90Mike Lockwood *      http://www.apache.org/licenses/LICENSE-2.0
946d0adf8256a42416584765625852b6e48497c90Mike Lockwood *
1046d0adf8256a42416584765625852b6e48497c90Mike Lockwood * Unless required by applicable law or agreed to in writing, software
1146d0adf8256a42416584765625852b6e48497c90Mike Lockwood * distributed under the License is distributed on an "AS IS" BASIS,
1246d0adf8256a42416584765625852b6e48497c90Mike Lockwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1346d0adf8256a42416584765625852b6e48497c90Mike Lockwood * See the License for the specific language governing permissions an
1446d0adf8256a42416584765625852b6e48497c90Mike Lockwood * limitations under the License.
1546d0adf8256a42416584765625852b6e48497c90Mike Lockwood */
1646d0adf8256a42416584765625852b6e48497c90Mike Lockwood
1746d0adf8256a42416584765625852b6e48497c90Mike Lockwoodpackage com.android.server.usb;
1846d0adf8256a42416584765625852b6e48497c90Mike Lockwood
19541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwoodimport android.app.Notification;
20541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwoodimport android.app.NotificationManager;
21fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkeyimport android.app.PendingIntent;
22d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwoodimport android.content.BroadcastReceiver;
23541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwoodimport android.content.ComponentName;
2446d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.content.ContentResolver;
2546d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.content.Context;
2646d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.content.Intent;
2746d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.content.IntentFilter;
2846d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.content.pm.PackageManager;
29541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwoodimport android.content.res.Resources;
30541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwoodimport android.database.ContentObserver;
3146d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.hardware.usb.UsbAccessory;
3246d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.hardware.usb.UsbManager;
3302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwoodimport android.os.FileUtils;
3446d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.os.Handler;
3502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwoodimport android.os.HandlerThread;
3602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwoodimport android.os.Looper;
3746d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.os.Message;
3846d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.os.ParcelFileDescriptor;
3902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwoodimport android.os.Process;
40dab2072365565b4892be7910b0cdb870e83689f6RoboErikimport android.os.SystemClock;
41541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwoodimport android.os.SystemProperties;
4246d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.os.UEventObserver;
43fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkeyimport android.os.UserHandle;
44fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkeyimport android.os.storage.StorageManager;
45fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkeyimport android.os.storage.StorageVolume;
4646d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.provider.Settings;
47afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondoimport android.util.Pair;
4846d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport android.util.Slog;
4946d0adf8256a42416584765625852b6e48497c90Mike Lockwood
508b2c3a14603d163d7564e6f60286995079687690Jeff Sharkeyimport com.android.internal.annotations.GuardedBy;
518b2c3a14603d163d7564e6f60286995079687690Jeff Sharkey
5246d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport java.io.File;
5346d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport java.io.FileDescriptor;
5446d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport java.io.FileNotFoundException;
5502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwoodimport java.io.IOException;
5646d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport java.io.PrintWriter;
57fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkeyimport java.util.HashMap;
58afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondoimport java.util.LinkedList;
5946d0adf8256a42416584765625852b6e48497c90Mike Lockwoodimport java.util.List;
60afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondoimport java.util.Map;
619d5a4be05b6d51ebb768e5fbcfa27ed9a39269b3Mike Lockwoodimport java.util.Scanner;
6246d0adf8256a42416584765625852b6e48497c90Mike Lockwood
6346d0adf8256a42416584765625852b6e48497c90Mike Lockwood/**
6446d0adf8256a42416584765625852b6e48497c90Mike Lockwood * UsbDeviceManager manages USB state in device mode.
6546d0adf8256a42416584765625852b6e48497c90Mike Lockwood */
6646d0adf8256a42416584765625852b6e48497c90Mike Lockwoodpublic class UsbDeviceManager {
6702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
6846d0adf8256a42416584765625852b6e48497c90Mike Lockwood    private static final String TAG = UsbDeviceManager.class.getSimpleName();
69fdc0c2984d05e32954608f46514c4cbe3a5a9424Mike Lockwood    private static final boolean DEBUG = false;
7046d0adf8256a42416584765625852b6e48497c90Mike Lockwood
7102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private static final String USB_STATE_MATCH =
7202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            "DEVPATH=/devices/virtual/android_usb/android0";
7302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private static final String ACCESSORY_START_MATCH =
7402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            "DEVPATH=/devices/virtual/misc/usb_accessory";
7502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private static final String FUNCTIONS_PATH =
7602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            "/sys/class/android_usb/android0/functions";
7702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private static final String STATE_PATH =
7802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            "/sys/class/android_usb/android0/state";
7902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private static final String MASS_STORAGE_FILE_PATH =
80629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood            "/sys/class/android_usb/android0/f_mass_storage/lun/file";
81629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood    private static final String RNDIS_ETH_ADDR_PATH =
82629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood            "/sys/class/android_usb/android0/f_rndis/ethaddr";
839d5a4be05b6d51ebb768e5fbcfa27ed9a39269b3Mike Lockwood    private static final String AUDIO_SOURCE_PCM_PATH =
849d5a4be05b6d51ebb768e5fbcfa27ed9a39269b3Mike Lockwood            "/sys/class/android_usb/android0/f_audio_source/pcm";
8546d0adf8256a42416584765625852b6e48497c90Mike Lockwood
8646d0adf8256a42416584765625852b6e48497c90Mike Lockwood    private static final int MSG_UPDATE_STATE = 0;
8702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private static final int MSG_ENABLE_ADB = 1;
88166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood    private static final int MSG_SET_CURRENT_FUNCTIONS = 2;
89f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood    private static final int MSG_SYSTEM_READY = 3;
90d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood    private static final int MSG_BOOT_COMPLETED = 4;
9127bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey    private static final int MSG_USER_SWITCHED = 5;
9246d0adf8256a42416584765625852b6e48497c90Mike Lockwood
93166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood    private static final int AUDIO_MODE_NONE = 0;
94166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood    private static final int AUDIO_MODE_SOURCE = 1;
95166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood
9646d0adf8256a42416584765625852b6e48497c90Mike Lockwood    // Delay for debouncing USB disconnects.
9746d0adf8256a42416584765625852b6e48497c90Mike Lockwood    // We often get rapid connect/disconnect events when enabling USB functions,
9846d0adf8256a42416584765625852b6e48497c90Mike Lockwood    // which need debouncing.
9946d0adf8256a42416584765625852b6e48497c90Mike Lockwood    private static final int UPDATE_DELAY = 1000;
10046d0adf8256a42416584765625852b6e48497c90Mike Lockwood
101afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo    private static final String BOOT_MODE_PROPERTY = "ro.bootmode";
102afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
10302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private UsbHandler mHandler;
104d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood    private boolean mBootCompleted;
10546d0adf8256a42416584765625852b6e48497c90Mike Lockwood
106fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey    private final Object mLock = new Object();
107fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey
10846d0adf8256a42416584765625852b6e48497c90Mike Lockwood    private final Context mContext;
10902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private final ContentResolver mContentResolver;
1108b2c3a14603d163d7564e6f60286995079687690Jeff Sharkey    @GuardedBy("mLock")
111fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey    private UsbSettingsManager mCurrentSettings;
112541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood    private NotificationManager mNotificationManager;
11346d0adf8256a42416584765625852b6e48497c90Mike Lockwood    private final boolean mHasUsbAccessory;
1145787a2d5b4e5bd60087eb7fbb13c97c7d0ba113eMike Lockwood    private boolean mUseUsbNotification;
115541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood    private boolean mAdbEnabled;
1169d5a4be05b6d51ebb768e5fbcfa27ed9a39269b3Mike Lockwood    private boolean mAudioSourceEnabled;
117afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo    private Map<String, List<Pair<String, String>>> mOemModeMap;
118166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood    private String[] mAccessoryStrings;
1194e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby    private UsbDebuggingManager mDebuggingManager;
120541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood
121541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood    private class AdbSettingsObserver extends ContentObserver {
122541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood        public AdbSettingsObserver() {
123541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood            super(null);
124541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood        }
125541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood        @Override
126541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood        public void onChange(boolean selfChange) {
127bf6f6f9de72c9fd15e6bda9f228c05a9b37d6324Jeff Brown            boolean enable = (Settings.Global.getInt(mContentResolver,
128bf6f6f9de72c9fd15e6bda9f228c05a9b37d6324Jeff Brown                    Settings.Global.ADB_ENABLED, 0) > 0);
12902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            mHandler.sendMessage(MSG_ENABLE_ADB, enable);
130541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood        }
131541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood    }
132541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood
13346d0adf8256a42416584765625852b6e48497c90Mike Lockwood    /*
13446d0adf8256a42416584765625852b6e48497c90Mike Lockwood     * Listens for uevent messages from the kernel to monitor the USB state
13546d0adf8256a42416584765625852b6e48497c90Mike Lockwood     */
13646d0adf8256a42416584765625852b6e48497c90Mike Lockwood    private final UEventObserver mUEventObserver = new UEventObserver() {
13746d0adf8256a42416584765625852b6e48497c90Mike Lockwood        @Override
13846d0adf8256a42416584765625852b6e48497c90Mike Lockwood        public void onUEvent(UEventObserver.UEvent event) {
139fdc0c2984d05e32954608f46514c4cbe3a5a9424Mike Lockwood            if (DEBUG) Slog.v(TAG, "USB UEVENT: " + event.toString());
14046d0adf8256a42416584765625852b6e48497c90Mike Lockwood
14102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            String state = event.get("USB_STATE");
14202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            String accessory = event.get("ACCESSORY");
14302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            if (state != null) {
14402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                mHandler.updateState(state);
14502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            } else if ("START".equals(accessory)) {
146fdc0c2984d05e32954608f46514c4cbe3a5a9424Mike Lockwood                if (DEBUG) Slog.d(TAG, "got accessory start");
147166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood                startAccessoryMode();
14846d0adf8256a42416584765625852b6e48497c90Mike Lockwood            }
14946d0adf8256a42416584765625852b6e48497c90Mike Lockwood        }
15046d0adf8256a42416584765625852b6e48497c90Mike Lockwood    };
15146d0adf8256a42416584765625852b6e48497c90Mike Lockwood
152fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey    public UsbDeviceManager(Context context) {
15346d0adf8256a42416584765625852b6e48497c90Mike Lockwood        mContext = context;
154541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood        mContentResolver = context.getContentResolver();
15546d0adf8256a42416584765625852b6e48497c90Mike Lockwood        PackageManager pm = mContext.getPackageManager();
15646d0adf8256a42416584765625852b6e48497c90Mike Lockwood        mHasUsbAccessory = pm.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY);
157629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        initRndisAddress();
15846d0adf8256a42416584765625852b6e48497c90Mike Lockwood
159afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        readOemUsbOverrideConfig();
160afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
16102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        // create a thread for our Handler
16202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        HandlerThread thread = new HandlerThread("UsbDeviceManager",
1633fd13eb6322e09f1ffe5476e28d55732da391151Mike Lockwood                Process.THREAD_PRIORITY_BACKGROUND);
16402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        thread.start();
1653fd13eb6322e09f1ffe5476e28d55732da391151Mike Lockwood        mHandler = new UsbHandler(thread.getLooper());
166d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood
167d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood        if (nativeIsStartRequested()) {
168d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood            if (DEBUG) Slog.d(TAG, "accessory attached at boot");
169166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood            startAccessoryMode();
170d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood        }
1714e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby
17278a9687fc2eab03b9d764cd27eaa1d56c66960beBenoit Goby        boolean secureAdbEnabled = SystemProperties.getBoolean("ro.adb.secure", false);
17378a9687fc2eab03b9d764cd27eaa1d56c66960beBenoit Goby        boolean dataEncrypted = "1".equals(SystemProperties.get("vold.decrypt"));
17478a9687fc2eab03b9d764cd27eaa1d56c66960beBenoit Goby        if (secureAdbEnabled && !dataEncrypted) {
1754e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby            mDebuggingManager = new UsbDebuggingManager(context);
1764e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby        }
17702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    }
17802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
179fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey    public void setCurrentSettings(UsbSettingsManager settings) {
180fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey        synchronized (mLock) {
181fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey            mCurrentSettings = settings;
182fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey        }
183fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey    }
184fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey
185fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey    private UsbSettingsManager getCurrentSettings() {
186fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey        synchronized (mLock) {
187fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey            return mCurrentSettings;
188fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey        }
189fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey    }
190fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey
19102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    public void systemReady() {
192d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood        if (DEBUG) Slog.d(TAG, "systemReady");
19302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
19402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        mNotificationManager = (NotificationManager)
19502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                mContext.getSystemService(Context.NOTIFICATION_SERVICE);
19646d0adf8256a42416584765625852b6e48497c90Mike Lockwood
1975787a2d5b4e5bd60087eb7fbb13c97c7d0ba113eMike Lockwood        // We do not show the USB notification if the primary volume supports mass storage.
1985787a2d5b4e5bd60087eb7fbb13c97c7d0ba113eMike Lockwood        // The legacy mass storage UI will be used instead.
1995787a2d5b4e5bd60087eb7fbb13c97c7d0ba113eMike Lockwood        boolean massStorageSupported = false;
200b049e212ab7fe8967893c202efcb30fecfdb82fbJeff Sharkey        final StorageManager storageManager = StorageManager.from(mContext);
201b049e212ab7fe8967893c202efcb30fecfdb82fbJeff Sharkey        final StorageVolume primary = storageManager.getPrimaryVolume();
202b049e212ab7fe8967893c202efcb30fecfdb82fbJeff Sharkey        massStorageSupported = primary != null && primary.allowMassStorage();
2035787a2d5b4e5bd60087eb7fbb13c97c7d0ba113eMike Lockwood        mUseUsbNotification = !massStorageSupported;
2045787a2d5b4e5bd60087eb7fbb13c97c7d0ba113eMike Lockwood
20502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        // make sure the ADB_ENABLED setting value matches the current state
206bf6f6f9de72c9fd15e6bda9f228c05a9b37d6324Jeff Brown        Settings.Global.putInt(mContentResolver, Settings.Global.ADB_ENABLED, mAdbEnabled ? 1 : 0);
207541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood
20802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        mHandler.sendEmptyMessage(MSG_SYSTEM_READY);
20902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    }
210541c9949ae9c05636d3e0442aa5080815121a042Mike Lockwood
211166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood    private void startAccessoryMode() {
212166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        mAccessoryStrings = nativeGetAccessoryStrings();
213166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        boolean enableAudio = (nativeGetAudioMode() == AUDIO_MODE_SOURCE);
214166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        // don't start accessory mode if our mandatory strings have not been set
215166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        boolean enableAccessory = (mAccessoryStrings != null &&
216166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood                        mAccessoryStrings[UsbAccessory.MANUFACTURER_STRING] != null &&
217166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood                        mAccessoryStrings[UsbAccessory.MODEL_STRING] != null);
218166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        String functions = null;
219166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood
220166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        if (enableAccessory && enableAudio) {
221166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood            functions = UsbManager.USB_FUNCTION_ACCESSORY + ","
222166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood                    + UsbManager.USB_FUNCTION_AUDIO_SOURCE;
223166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        } else if (enableAccessory) {
224166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood            functions = UsbManager.USB_FUNCTION_ACCESSORY;
225166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        } else if (enableAudio) {
226166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood            functions = UsbManager.USB_FUNCTION_AUDIO_SOURCE;
227166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        }
228166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood
229166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        if (functions != null) {
230166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood            setCurrentFunctions(functions, false);
231166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        }
232166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood    }
233166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood
234629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood    private static void initRndisAddress() {
235629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        // configure RNDIS ethernet address based on our serial number using the same algorithm
236629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        // we had been previously using in kernel board files
237629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        final int ETH_ALEN = 6;
238629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        int address[] = new int[ETH_ALEN];
239629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        // first byte is 0x02 to signify a locally administered address
240629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        address[0] = 0x02;
241629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood
242629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        String serial = SystemProperties.get("ro.serialno", "1234567890ABCDEF");
243629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        int serialLength = serial.length();
244629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        // XOR the USB serial across the remaining 5 bytes
245629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        for (int i = 0; i < serialLength; i++) {
246629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood            address[i % (ETH_ALEN - 1) + 1] ^= (int)serial.charAt(i);
247629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        }
248629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        String addrString = String.format("%02X:%02X:%02X:%02X:%02X:%02X",
249629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood            address[0], address[1], address[2], address[3], address[4], address[5]);
250629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        try {
251629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood            FileUtils.stringToFile(RNDIS_ETH_ADDR_PATH, addrString);
252629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        } catch (IOException e) {
253629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood           Slog.e(TAG, "failed to write to " + RNDIS_ETH_ADDR_PATH);
254629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood        }
255629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood    }
256629b149ff584bc247f68014a676101d1d5787d27Mike Lockwood
25702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood     private static String addFunction(String functions, String function) {
258dab2072365565b4892be7910b0cdb870e83689f6RoboErik         if ("none".equals(functions)) {
259dab2072365565b4892be7910b0cdb870e83689f6RoboErik             return function;
260dab2072365565b4892be7910b0cdb870e83689f6RoboErik         }
26102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        if (!containsFunction(functions, function)) {
26202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            if (functions.length() > 0) {
26302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                functions += ",";
26446d0adf8256a42416584765625852b6e48497c90Mike Lockwood            }
26502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            functions += function;
26646d0adf8256a42416584765625852b6e48497c90Mike Lockwood        }
26702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        return functions;
26846d0adf8256a42416584765625852b6e48497c90Mike Lockwood    }
26946d0adf8256a42416584765625852b6e48497c90Mike Lockwood
27002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private static String removeFunction(String functions, String function) {
27102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        String[] split = functions.split(",");
27202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        for (int i = 0; i < split.length; i++) {
27302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            if (function.equals(split[i])) {
27402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                split[i] = null;
27502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            }
27602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        }
277dab2072365565b4892be7910b0cdb870e83689f6RoboErik        if (split.length == 1 && split[0] == null) {
278dab2072365565b4892be7910b0cdb870e83689f6RoboErik            return "none";
279dab2072365565b4892be7910b0cdb870e83689f6RoboErik        }
28002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        StringBuilder builder = new StringBuilder();
28102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood         for (int i = 0; i < split.length; i++) {
28202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            String s = split[i];
28302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            if (s != null) {
28402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                if (builder.length() > 0) {
28502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    builder.append(",");
28602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                }
28702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                builder.append(s);
28802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            }
28902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        }
29002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        return builder.toString();
29102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    }
29246d0adf8256a42416584765625852b6e48497c90Mike Lockwood
29302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private static boolean containsFunction(String functions, String function) {
29402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        int index = functions.indexOf(function);
29502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        if (index < 0) return false;
29602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        if (index > 0 && functions.charAt(index - 1) != ',') return false;
29702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        int charAfter = index + function.length();
29802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        if (charAfter < functions.length() && functions.charAt(charAfter) != ',') return false;
29902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        return true;
30002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    }
30102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
30202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    private final class UsbHandler extends Handler {
30302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
30402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        // current USB state
30502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        private boolean mConnected;
30602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        private boolean mConfigured;
30702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        private String mCurrentFunctions;
30802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        private String mDefaultFunctions;
30902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        private UsbAccessory mCurrentAccessory;
310d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood        private int mUsbNotificationId;
311d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood        private boolean mAdbNotificationShown;
31227bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey        private int mCurrentUser = UserHandle.USER_NULL;
313d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood
314d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood        private final BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() {
31527bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey            @Override
316d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood            public void onReceive(Context context, Intent intent) {
317d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood                if (DEBUG) Slog.d(TAG, "boot completed");
318d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood                mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
319d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood            }
320d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood        };
321d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood
32227bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey        private final BroadcastReceiver mUserSwitchedReceiver = new BroadcastReceiver() {
32327bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey            @Override
32427bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey            public void onReceive(Context context, Intent intent) {
32527bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
32627bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                mHandler.obtainMessage(MSG_USER_SWITCHED, userId, 0).sendToTarget();
32727bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey            }
32827bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey        };
32927bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey
3303fd13eb6322e09f1ffe5476e28d55732da391151Mike Lockwood        public UsbHandler(Looper looper) {
3313fd13eb6322e09f1ffe5476e28d55732da391151Mike Lockwood            super(looper);
33202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            try {
333c264afeb5528733a215a472e761b51cc59bba454Mike Lockwood                // persist.sys.usb.config should never be unset.  But if it is, set it to "adb"
334c264afeb5528733a215a472e761b51cc59bba454Mike Lockwood                // so we have a chance of debugging what happened.
335c264afeb5528733a215a472e761b51cc59bba454Mike Lockwood                mDefaultFunctions = SystemProperties.get("persist.sys.usb.config", "adb");
336afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
337afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                // Check if USB mode needs to be overridden depending on OEM specific bootmode.
338afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                mDefaultFunctions = processOemUsbOverride(mDefaultFunctions);
339afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
340de296f64483713fdf164f3e8bf41dc12d1cff59eMike Lockwood                // sanity check the sys.usb.config system property
341de296f64483713fdf164f3e8bf41dc12d1cff59eMike Lockwood                // this may be necessary if we crashed while switching USB configurations
342de296f64483713fdf164f3e8bf41dc12d1cff59eMike Lockwood                String config = SystemProperties.get("sys.usb.config", "none");
343c264afeb5528733a215a472e761b51cc59bba454Mike Lockwood                if (!config.equals(mDefaultFunctions)) {
344c264afeb5528733a215a472e761b51cc59bba454Mike Lockwood                    Slog.w(TAG, "resetting config to persistent property: " + mDefaultFunctions);
345c264afeb5528733a215a472e761b51cc59bba454Mike Lockwood                    SystemProperties.set("sys.usb.config", mDefaultFunctions);
346de296f64483713fdf164f3e8bf41dc12d1cff59eMike Lockwood                }
347de296f64483713fdf164f3e8bf41dc12d1cff59eMike Lockwood
348c264afeb5528733a215a472e761b51cc59bba454Mike Lockwood                mCurrentFunctions = mDefaultFunctions;
34902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim();
35002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                updateState(state);
35102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                mAdbEnabled = containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ADB);
35202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
35302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                // Upgrade step for previous versions that used persist.service.adb.enable
35402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                String value = SystemProperties.get("persist.service.adb.enable", "");
35502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                if (value.length() > 0) {
35602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    char enable = value.charAt(0);
35702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    if (enable == '1') {
35802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                        setAdbEnabled(true);
35902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    } else if (enable == '0') {
36002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                        setAdbEnabled(false);
36102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    }
36202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    SystemProperties.set("persist.service.adb.enable", "");
36302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                }
36402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
36502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                // register observer to listen for settings changes
36602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                mContentResolver.registerContentObserver(
367bf6f6f9de72c9fd15e6bda9f228c05a9b37d6324Jeff Brown                        Settings.Global.getUriFor(Settings.Global.ADB_ENABLED),
36802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                                false, new AdbSettingsObserver());
36902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
37002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                // Watch for USB configuration changes
37102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                mUEventObserver.startObserving(USB_STATE_MATCH);
37202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                mUEventObserver.startObserving(ACCESSORY_START_MATCH);
373d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood
37427bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                mContext.registerReceiver(
37527bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                        mBootCompletedReceiver, new IntentFilter(Intent.ACTION_BOOT_COMPLETED));
37627bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                mContext.registerReceiver(
37727bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                        mUserSwitchedReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
37802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            } catch (Exception e) {
37902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                Slog.e(TAG, "Error initializing UsbHandler", e);
38002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            }
38146d0adf8256a42416584765625852b6e48497c90Mike Lockwood        }
38202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
38302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        public void sendMessage(int what, boolean arg) {
38402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            removeMessages(what);
38502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            Message m = Message.obtain(this, what);
38602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            m.arg1 = (arg ? 1 : 0);
38702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            sendMessage(m);
38846d0adf8256a42416584765625852b6e48497c90Mike Lockwood        }
38946d0adf8256a42416584765625852b6e48497c90Mike Lockwood
39002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        public void sendMessage(int what, Object arg) {
39102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            removeMessages(what);
39202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            Message m = Message.obtain(this, what);
39302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            m.obj = arg;
39402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            sendMessage(m);
39502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        }
39602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
397f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood        public void sendMessage(int what, Object arg0, boolean arg1) {
398f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood            removeMessages(what);
399f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood            Message m = Message.obtain(this, what);
400f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood            m.obj = arg0;
401f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood            m.arg1 = (arg1 ? 1 : 0);
402f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood            sendMessage(m);
403f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood        }
404f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood
40502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        public void updateState(String state) {
40602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            int connected, configured;
40702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
40802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            if ("DISCONNECTED".equals(state)) {
40902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                connected = 0;
41002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                configured = 0;
41102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            } else if ("CONNECTED".equals(state)) {
41202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                connected = 1;
41302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                configured = 0;
41402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            } else if ("CONFIGURED".equals(state)) {
41502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                connected = 1;
41602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                configured = 1;
41702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            } else {
41802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                Slog.e(TAG, "unknown state " + state);
41902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                return;
42002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            }
42102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            removeMessages(MSG_UPDATE_STATE);
42202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            Message msg = Message.obtain(this, MSG_UPDATE_STATE);
42302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            msg.arg1 = connected;
42402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            msg.arg2 = configured;
42502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            // debounce disconnects to avoid problems bringing up USB tethering
42602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            sendMessageDelayed(msg, (connected == 0) ? UPDATE_DELAY : 0);
42702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        }
42802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
429f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood        private boolean waitForState(String state) {
43002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            // wait for the transition to complete.
43102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            // give up after 1 second.
43202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            for (int i = 0; i < 20; i++) {
43368736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                // State transition is done when sys.usb.state is set to the new configuration
434f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood                if (state.equals(SystemProperties.get("sys.usb.state"))) return true;
435dab2072365565b4892be7910b0cdb870e83689f6RoboErik                SystemClock.sleep(50);
43602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            }
437fdc0c2984d05e32954608f46514c4cbe3a5a9424Mike Lockwood            Slog.e(TAG, "waitForState(" + state + ") FAILED");
43802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            return false;
43902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        }
44002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
441f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood        private boolean setUsbConfig(String config) {
442fdc0c2984d05e32954608f46514c4cbe3a5a9424Mike Lockwood            if (DEBUG) Slog.d(TAG, "setUsbConfig(" + config + ")");
443f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood            // set the new configuration
444f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood            SystemProperties.set("sys.usb.config", config);
445f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood            return waitForState(config);
446f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood        }
447f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood
44802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        private void setAdbEnabled(boolean enable) {
449de296f64483713fdf164f3e8bf41dc12d1cff59eMike Lockwood            if (DEBUG) Slog.d(TAG, "setAdbEnabled: " + enable);
45002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            if (enable != mAdbEnabled) {
45102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                mAdbEnabled = enable;
452f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood                // Due to the persist.sys.usb.config property trigger, changing adb state requires
453f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood                // switching to default function
4541984e79d1593b80adf46b16666862f5787157aedMike Lockwood                setEnabledFunctions(mDefaultFunctions, true);
455d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                updateAdbNotification();
45602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            }
4574e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby            if (mDebuggingManager != null) {
4584e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby                mDebuggingManager.setAdbEnabled(mAdbEnabled);
4594e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby            }
46002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        }
46102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
46268736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood        private void setEnabledFunctions(String functions, boolean makeDefault) {
463afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
464afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo            // Do not update persystent.sys.usb.config if the device is booted up
465afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo            // with OEM specific mode.
466afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo            if (functions != null && makeDefault && !needsOemUsbOverride()) {
467afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
4688b4d36e8661e25fcb893e00be15ba02da0950055Mike Lockwood                if (mAdbEnabled) {
4698b4d36e8661e25fcb893e00be15ba02da0950055Mike Lockwood                    functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB);
4708b4d36e8661e25fcb893e00be15ba02da0950055Mike Lockwood                } else {
4718b4d36e8661e25fcb893e00be15ba02da0950055Mike Lockwood                    functions = removeFunction(functions, UsbManager.USB_FUNCTION_ADB);
4728b4d36e8661e25fcb893e00be15ba02da0950055Mike Lockwood                }
47368736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                if (!mDefaultFunctions.equals(functions)) {
47468736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    if (!setUsbConfig("none")) {
47568736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        Slog.e(TAG, "Failed to disable USB");
47668736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        // revert to previous configuration if we fail
47768736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        setUsbConfig(mCurrentFunctions);
47868736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        return;
47968736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    }
48068736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    // setting this property will also change the current USB state
48168736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    // via a property trigger
48268736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    SystemProperties.set("persist.sys.usb.config", functions);
48368736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    if (waitForState(functions)) {
48468736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        mCurrentFunctions = functions;
48568736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        mDefaultFunctions = functions;
48668736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    } else {
48768736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        Slog.e(TAG, "Failed to switch persistent USB config to " + functions);
48868736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        // revert to previous configuration if we fail
48968736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        SystemProperties.set("persist.sys.usb.config", mDefaultFunctions);
49068736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    }
49168736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                }
49268736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood            } else {
49368736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                if (functions == null) {
49468736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    functions = mDefaultFunctions;
49568736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                }
496afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
497afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                // Override with bootmode specific usb mode if needed
498afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                functions = processOemUsbOverride(functions);
499afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
5008b4d36e8661e25fcb893e00be15ba02da0950055Mike Lockwood                if (mAdbEnabled) {
5018b4d36e8661e25fcb893e00be15ba02da0950055Mike Lockwood                    functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB);
5028b4d36e8661e25fcb893e00be15ba02da0950055Mike Lockwood                } else {
5038b4d36e8661e25fcb893e00be15ba02da0950055Mike Lockwood                    functions = removeFunction(functions, UsbManager.USB_FUNCTION_ADB);
5048b4d36e8661e25fcb893e00be15ba02da0950055Mike Lockwood                }
50568736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                if (!mCurrentFunctions.equals(functions)) {
50668736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    if (!setUsbConfig("none")) {
50768736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        Slog.e(TAG, "Failed to disable USB");
50868736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        // revert to previous configuration if we fail
50968736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        setUsbConfig(mCurrentFunctions);
51068736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        return;
51168736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    }
51268736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    if (setUsbConfig(functions)) {
51368736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        mCurrentFunctions = functions;
51468736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    } else {
51568736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        Slog.e(TAG, "Failed to switch USB config to " + functions);
51668736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        // revert to previous configuration if we fail
51768736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        setUsbConfig(mCurrentFunctions);
51868736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                    }
51968736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                }
52002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            }
52102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        }
52202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
52302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        private void updateCurrentAccessory() {
52402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            if (!mHasUsbAccessory) return;
52502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
52602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            if (mConfigured) {
527166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood                if (mAccessoryStrings != null) {
528166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood                    mCurrentAccessory = new UsbAccessory(mAccessoryStrings);
529fdc0c2984d05e32954608f46514c4cbe3a5a9424Mike Lockwood                    Slog.d(TAG, "entering USB accessory mode: " + mCurrentAccessory);
53002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    // defer accessoryAttached if system is not ready
531d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood                    if (mBootCompleted) {
532fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey                        getCurrentSettings().accessoryAttached(mCurrentAccessory);
533d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood                    } // else handle in mBootCompletedReceiver
53402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                } else {
535fdc0c2984d05e32954608f46514c4cbe3a5a9424Mike Lockwood                    Slog.e(TAG, "nativeGetAccessoryStrings failed");
53602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                }
53702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            } else if (!mConnected) {
53802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                // make sure accessory mode is off
53902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                // and restore default functions
540fdc0c2984d05e32954608f46514c4cbe3a5a9424Mike Lockwood                Slog.d(TAG, "exited USB accessory mode");
54168736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                setEnabledFunctions(mDefaultFunctions, false);
54202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
54302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                if (mCurrentAccessory != null) {
544d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood                    if (mBootCompleted) {
545fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey                        getCurrentSettings().accessoryDetached(mCurrentAccessory);
54602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    }
54702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    mCurrentAccessory = null;
548166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood                    mAccessoryStrings = null;
54946d0adf8256a42416584765625852b6e48497c90Mike Lockwood                }
55046d0adf8256a42416584765625852b6e48497c90Mike Lockwood            }
55146d0adf8256a42416584765625852b6e48497c90Mike Lockwood        }
55246d0adf8256a42416584765625852b6e48497c90Mike Lockwood
55302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        private void updateUsbState() {
55402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            // send a sticky broadcast containing current USB state
55502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            Intent intent = new Intent(UsbManager.ACTION_USB_STATE);
55602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
55702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            intent.putExtra(UsbManager.USB_CONNECTED, mConnected);
55802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            intent.putExtra(UsbManager.USB_CONFIGURED, mConfigured);
55902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
56002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            if (mCurrentFunctions != null) {
56102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                String[] functions = mCurrentFunctions.split(",");
56202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                for (int i = 0; i < functions.length; i++) {
56302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    intent.putExtra(functions[i], true);
56402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                }
56546d0adf8256a42416584765625852b6e48497c90Mike Lockwood            }
56602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
5675ac72a29593ab9a20337a2225df52bdf4754be02Dianne Hackborn            mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
56846d0adf8256a42416584765625852b6e48497c90Mike Lockwood        }
56946d0adf8256a42416584765625852b6e48497c90Mike Lockwood
570bf91046872dd711019e8dec543441601576cc950Mike Lockwood        private void updateAudioSourceFunction() {
571bf91046872dd711019e8dec543441601576cc950Mike Lockwood            boolean enabled = containsFunction(mCurrentFunctions,
572bf91046872dd711019e8dec543441601576cc950Mike Lockwood                    UsbManager.USB_FUNCTION_AUDIO_SOURCE);
573bf91046872dd711019e8dec543441601576cc950Mike Lockwood            if (enabled != mAudioSourceEnabled) {
574bf91046872dd711019e8dec543441601576cc950Mike Lockwood                // send a sticky broadcast containing current USB state
575bf91046872dd711019e8dec543441601576cc950Mike Lockwood                Intent intent = new Intent(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG);
576bf91046872dd711019e8dec543441601576cc950Mike Lockwood                intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
577bf91046872dd711019e8dec543441601576cc950Mike Lockwood                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
578bf91046872dd711019e8dec543441601576cc950Mike Lockwood                intent.putExtra("state", (enabled ? 1 : 0));
579bf91046872dd711019e8dec543441601576cc950Mike Lockwood                if (enabled) {
580bf91046872dd711019e8dec543441601576cc950Mike Lockwood                    try {
581bf91046872dd711019e8dec543441601576cc950Mike Lockwood                        Scanner scanner = new Scanner(new File(AUDIO_SOURCE_PCM_PATH));
582bf91046872dd711019e8dec543441601576cc950Mike Lockwood                        int card = scanner.nextInt();
583bf91046872dd711019e8dec543441601576cc950Mike Lockwood                        int device = scanner.nextInt();
584bf91046872dd711019e8dec543441601576cc950Mike Lockwood                        intent.putExtra("card", card);
585bf91046872dd711019e8dec543441601576cc950Mike Lockwood                        intent.putExtra("device", device);
586bf91046872dd711019e8dec543441601576cc950Mike Lockwood                    } catch (FileNotFoundException e) {
587bf91046872dd711019e8dec543441601576cc950Mike Lockwood                        Slog.e(TAG, "could not open audio source PCM file", e);
588bf91046872dd711019e8dec543441601576cc950Mike Lockwood                    }
5899d5a4be05b6d51ebb768e5fbcfa27ed9a39269b3Mike Lockwood                }
5905ac72a29593ab9a20337a2225df52bdf4754be02Dianne Hackborn                mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
591bf91046872dd711019e8dec543441601576cc950Mike Lockwood                mAudioSourceEnabled = enabled;
5929d5a4be05b6d51ebb768e5fbcfa27ed9a39269b3Mike Lockwood            }
5939d5a4be05b6d51ebb768e5fbcfa27ed9a39269b3Mike Lockwood        }
5949d5a4be05b6d51ebb768e5fbcfa27ed9a39269b3Mike Lockwood
59502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        @Override
59602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        public void handleMessage(Message msg) {
59702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            switch (msg.what) {
59802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                case MSG_UPDATE_STATE:
59902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    mConnected = (msg.arg1 == 1);
60002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    mConfigured = (msg.arg2 == 1);
601d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    updateUsbNotification();
602d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    updateAdbNotification();
60302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    if (containsFunction(mCurrentFunctions,
60402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                            UsbManager.USB_FUNCTION_ACCESSORY)) {
60502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                        updateCurrentAccessory();
60602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    }
60702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
60802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    if (!mConnected) {
60902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                        // restore defaults when USB is disconnected
61068736cbf938935f7d7e1eb2b3f9ec911fcb0da72Mike Lockwood                        setEnabledFunctions(mDefaultFunctions, false);
61102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    }
612d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood                    if (mBootCompleted) {
61302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                        updateUsbState();
614bf91046872dd711019e8dec543441601576cc950Mike Lockwood                        updateAudioSourceFunction();
61502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    }
61602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    break;
61702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                case MSG_ENABLE_ADB:
61802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    setAdbEnabled(msg.arg1 == 1);
61902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    break;
620166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood                case MSG_SET_CURRENT_FUNCTIONS:
621166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood                    String functions = (String)msg.obj;
622f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood                    boolean makeDefault = (msg.arg1 == 1);
623166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood                    setEnabledFunctions(functions, makeDefault);
62402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    break;
62502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                case MSG_SYSTEM_READY:
626d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    updateUsbNotification();
627d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    updateAdbNotification();
62802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    updateUsbState();
629bf91046872dd711019e8dec543441601576cc950Mike Lockwood                    updateAudioSourceFunction();
630d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood                    break;
631d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood                case MSG_BOOT_COMPLETED:
632d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood                    mBootCompleted = true;
633d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood                    if (mCurrentAccessory != null) {
634fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey                        getCurrentSettings().accessoryAttached(mCurrentAccessory);
63502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    }
6364e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby                    if (mDebuggingManager != null) {
6374e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby                        mDebuggingManager.setAdbEnabled(mAdbEnabled);
6384e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby                    }
63902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood                    break;
64027bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                case MSG_USER_SWITCHED: {
64127bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                    final boolean mtpActive =
64227bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                            containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MTP)
64327bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                            || containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_PTP);
64427bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                    if (mtpActive && mCurrentUser != UserHandle.USER_NULL) {
64527bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                        Slog.v(TAG, "Current user switched; resetting USB host stack for MTP");
64627bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                        setUsbConfig("none");
64727bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                        setUsbConfig(mCurrentFunctions);
64827bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                    }
64927bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                    mCurrentUser = msg.arg1;
65027bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                    break;
65127bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                }
65246d0adf8256a42416584765625852b6e48497c90Mike Lockwood            }
65346d0adf8256a42416584765625852b6e48497c90Mike Lockwood        }
65446d0adf8256a42416584765625852b6e48497c90Mike Lockwood
65502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        public UsbAccessory getCurrentAccessory() {
65602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            return mCurrentAccessory;
65702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        }
65802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
659d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood        private void updateUsbNotification() {
660d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood            if (mNotificationManager == null || !mUseUsbNotification) return;
661fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood            int id = 0;
662a5010431bfcdff88ac9d96e68d8308071c13472bMike Lockwood            Resources r = mContext.getResources();
663d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood            if (mConnected) {
664d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                if (containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MTP)) {
665fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood                    id = com.android.internal.R.string.usb_mtp_notification_title;
666d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                } else if (containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_PTP)) {
667fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood                    id = com.android.internal.R.string.usb_ptp_notification_title;
668d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                } else if (containsFunction(mCurrentFunctions,
669d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                        UsbManager.USB_FUNCTION_MASS_STORAGE)) {
670fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood                    id = com.android.internal.R.string.usb_cd_installer_notification_title;
6716e680dea3bc9e2d4ba1a09f428c303cd2a59c051Mike Lockwood                } else if (containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ACCESSORY)) {
672fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood                    id = com.android.internal.R.string.usb_accessory_notification_title;
673d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                } else {
674a5010431bfcdff88ac9d96e68d8308071c13472bMike Lockwood                    // There is a different notification for USB tethering so we don't need one here
6757ff30113de68539ec840c524b8f1561f938f96c5Dianne Hackborn                    //if (!containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_RNDIS)) {
6767ff30113de68539ec840c524b8f1561f938f96c5Dianne Hackborn                    //    Slog.e(TAG, "No known USB function in updateUsbNotification");
6777ff30113de68539ec840c524b8f1561f938f96c5Dianne Hackborn                    //}
678d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                }
679a5010431bfcdff88ac9d96e68d8308071c13472bMike Lockwood            }
680a5010431bfcdff88ac9d96e68d8308071c13472bMike Lockwood            if (id != mUsbNotificationId) {
681a5010431bfcdff88ac9d96e68d8308071c13472bMike Lockwood                // clear notification if title needs changing
682fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood                if (mUsbNotificationId != 0) {
68350cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                    mNotificationManager.cancelAsUser(null, mUsbNotificationId,
68450cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                            UserHandle.ALL);
685fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood                    mUsbNotificationId = 0;
686a5010431bfcdff88ac9d96e68d8308071c13472bMike Lockwood                }
687fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood                if (id != 0) {
688d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    CharSequence message = r.getText(
689d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                            com.android.internal.R.string.usb_notification_message);
690fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood                    CharSequence title = r.getText(id);
691d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood
692d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    Notification notification = new Notification();
693d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.icon = com.android.internal.R.drawable.stat_sys_data_usb;
694d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.when = 0;
695d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.flags = Notification.FLAG_ONGOING_EVENT;
696d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.tickerText = title;
697d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.defaults = 0; // please be quiet
698d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.sound = null;
699d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.vibrate = null;
70049a2ad1f404622803faba0bc5937b8a39aba02b3Daniel Sandler                    notification.priority = Notification.PRIORITY_MIN;
701d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood
702765f97d5e608031bc1de4156c6e681e4d178c7eeChristopher Tate                    Intent intent = Intent.makeRestartActivityTask(
703765f97d5e608031bc1de4156c6e681e4d178c7eeChristopher Tate                            new ComponentName("com.android.settings",
704765f97d5e608031bc1de4156c6e681e4d178c7eeChristopher Tate                                    "com.android.settings.UsbSettings"));
70550cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                    PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0,
70650cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                            intent, 0, null, UserHandle.CURRENT);
707d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.setLatestEventInfo(mContext, title, message, pi);
70850cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                    mNotificationManager.notifyAsUser(null, id, notification,
70950cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                            UserHandle.ALL);
710d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    mUsbNotificationId = id;
711d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                }
712d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood            }
713d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood        }
714d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood
715d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood        private void updateAdbNotification() {
716d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood            if (mNotificationManager == null) return;
717fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood            final int id = com.android.internal.R.string.adb_active_notification_title;
718d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood            if (mAdbEnabled && mConnected) {
719d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                if ("0".equals(SystemProperties.get("persist.adb.notify"))) return;
720d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood
721d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                if (!mAdbNotificationShown) {
722d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    Resources r = mContext.getResources();
723fadd2b8e0ffea0d5b4bf3ca16af4d5cea001b561Mike Lockwood                    CharSequence title = r.getText(id);
724d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    CharSequence message = r.getText(
725d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                            com.android.internal.R.string.adb_active_notification_message);
726d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood
727d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    Notification notification = new Notification();
728d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.icon = com.android.internal.R.drawable.stat_sys_adb;
729d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.when = 0;
730d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.flags = Notification.FLAG_ONGOING_EVENT;
731d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.tickerText = title;
732d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.defaults = 0; // please be quiet
733d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.sound = null;
734d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.vibrate = null;
735a25079e6bafcfd77135a852f0e838738d80606efDaniel Sandler                    notification.priority = Notification.PRIORITY_LOW;
736d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood
737765f97d5e608031bc1de4156c6e681e4d178c7eeChristopher Tate                    Intent intent = Intent.makeRestartActivityTask(
738765f97d5e608031bc1de4156c6e681e4d178c7eeChristopher Tate                            new ComponentName("com.android.settings",
739765f97d5e608031bc1de4156c6e681e4d178c7eeChristopher Tate                                    "com.android.settings.DevelopmentSettings"));
74050cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                    PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0,
74150cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                            intent, 0, null, UserHandle.CURRENT);
742d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    notification.setLatestEventInfo(mContext, title, message, pi);
743d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                    mAdbNotificationShown = true;
74450cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                    mNotificationManager.notifyAsUser(null, id, notification,
74550cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                            UserHandle.ALL);
746d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                }
747d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood            } else if (mAdbNotificationShown) {
748d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood                mAdbNotificationShown = false;
74950cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                mNotificationManager.cancelAsUser(null, id, UserHandle.ALL);
750d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood            }
751d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood        }
752d8404d2fdd7036435748548a6791063fb6d6c909Mike Lockwood
75302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        public void dump(FileDescriptor fd, PrintWriter pw) {
75402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            pw.println("  USB Device State:");
75502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            pw.println("    Current Functions: " + mCurrentFunctions);
75602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            pw.println("    Default Functions: " + mDefaultFunctions);
75702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            pw.println("    mConnected: " + mConnected);
75802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            pw.println("    mConfigured: " + mConfigured);
75902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            pw.println("    mCurrentAccessory: " + mCurrentAccessory);
7606ea146c239f2468563b4e8766b446d14ae4a1ff5Mike Lockwood            try {
7616ea146c239f2468563b4e8766b446d14ae4a1ff5Mike Lockwood                pw.println("    Kernel state: "
7626ea146c239f2468563b4e8766b446d14ae4a1ff5Mike Lockwood                        + FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim());
7636ea146c239f2468563b4e8766b446d14ae4a1ff5Mike Lockwood                pw.println("    Kernel function list: "
7646ea146c239f2468563b4e8766b446d14ae4a1ff5Mike Lockwood                        + FileUtils.readTextFile(new File(FUNCTIONS_PATH), 0, null).trim());
7656ea146c239f2468563b4e8766b446d14ae4a1ff5Mike Lockwood                pw.println("    Mass storage backing file: "
7666ea146c239f2468563b4e8766b446d14ae4a1ff5Mike Lockwood                        + FileUtils.readTextFile(new File(MASS_STORAGE_FILE_PATH), 0, null).trim());
7676ea146c239f2468563b4e8766b446d14ae4a1ff5Mike Lockwood            } catch (IOException e) {
7686ea146c239f2468563b4e8766b446d14ae4a1ff5Mike Lockwood                pw.println("IOException: " + e);
7696ea146c239f2468563b4e8766b446d14ae4a1ff5Mike Lockwood            }
77002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        }
77146d0adf8256a42416584765625852b6e48497c90Mike Lockwood    }
77246d0adf8256a42416584765625852b6e48497c90Mike Lockwood
77302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    /* returns the currently attached USB accessory */
77446d0adf8256a42416584765625852b6e48497c90Mike Lockwood    public UsbAccessory getCurrentAccessory() {
77502e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        return mHandler.getCurrentAccessory();
77646d0adf8256a42416584765625852b6e48497c90Mike Lockwood    }
77746d0adf8256a42416584765625852b6e48497c90Mike Lockwood
77802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    /* opens the currently attached USB accessory */
779abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood    public ParcelFileDescriptor openAccessory(UsbAccessory accessory) {
780abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood        UsbAccessory currentAccessory = mHandler.getCurrentAccessory();
781abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood        if (currentAccessory == null) {
782abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood            throw new IllegalArgumentException("no accessory attached");
78346d0adf8256a42416584765625852b6e48497c90Mike Lockwood        }
784abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood        if (!currentAccessory.equals(accessory)) {
785abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood            String error = accessory.toString()
786abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood                    + " does not match current accessory "
787abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood                    + currentAccessory;
788abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood            throw new IllegalArgumentException(error);
789abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood        }
790fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey        getCurrentSettings().checkPermission(accessory);
791abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood        return nativeOpenAccessory();
792abc4ac6d48c52bd8b69026441bf261e5c68c24f4Mike Lockwood    }
79302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood
794166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood    public void setCurrentFunctions(String functions, boolean makeDefault) {
795166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        if (DEBUG) Slog.d(TAG, "setCurrentFunctions(" + functions + ") default: " + makeDefault);
796166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood        mHandler.sendMessage(MSG_SET_CURRENT_FUNCTIONS, functions, makeDefault);
79702e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    }
79846d0adf8256a42416584765625852b6e48497c90Mike Lockwood
79902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    public void setMassStorageBackingFile(String path) {
80002e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        if (path == null) path = "";
80102e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        try {
80202e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            FileUtils.stringToFile(MASS_STORAGE_FILE_PATH, path);
80302e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        } catch (IOException e) {
80402e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood           Slog.e(TAG, "failed to write to " + MASS_STORAGE_FILE_PATH);
80546d0adf8256a42416584765625852b6e48497c90Mike Lockwood        }
80602e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood    }
80746d0adf8256a42416584765625852b6e48497c90Mike Lockwood
808afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo    private void readOemUsbOverrideConfig() {
809afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        String[] configList = mContext.getResources().getStringArray(
810afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo            com.android.internal.R.array.config_oemUsbModeOverride);
811afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
812afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        if (configList != null) {
813afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo            for (String config: configList) {
814afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                String[] items = config.split(":");
815afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                if (items.length == 3) {
816afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                    if (mOemModeMap == null) {
817afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                        mOemModeMap = new HashMap<String, List<Pair<String, String>>>();
818afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                    }
819afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                    List overrideList = mOemModeMap.get(items[0]);
820afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                    if (overrideList == null) {
821afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                        overrideList = new LinkedList<Pair<String, String>>();
822afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                        mOemModeMap.put(items[0], overrideList);
823afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                    }
824afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                    overrideList.add(new Pair<String, String>(items[1], items[2]));
825afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                }
826afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo            }
827afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        }
828afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo    }
829afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
830afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo    private boolean needsOemUsbOverride() {
831afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        if (mOemModeMap == null) return false;
832afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
833afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        String bootMode = SystemProperties.get(BOOT_MODE_PROPERTY, "unknown");
834afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        return (mOemModeMap.get(bootMode) != null) ? true : false;
835afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo    }
836afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
837afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo    private String processOemUsbOverride(String usbFunctions) {
838afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        if ((usbFunctions == null) || (mOemModeMap == null)) return usbFunctions;
839afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
840afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        String bootMode = SystemProperties.get(BOOT_MODE_PROPERTY, "unknown");
841afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
842afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        List<Pair<String, String>> overrides = mOemModeMap.get(bootMode);
843afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        if (overrides != null) {
844afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo            for (Pair<String, String> pair: overrides) {
845afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                if (pair.first.equals(usbFunctions)) {
846afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                    Slog.d(TAG, "OEM USB override: " + pair.first + " ==> " + pair.second);
847afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                    return pair.second;
848afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo                }
849afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo            }
850afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        }
851afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        // return passed in functions as is.
852afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo        return usbFunctions;
853afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo    }
854afd8f1820eeee21e960be44b22113dc2cecacf98Kazuhiro Ondo
8554e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby    public void allowUsbDebugging(boolean alwaysAllow, String publicKey) {
8564e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby        if (mDebuggingManager != null) {
8574e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby            mDebuggingManager.allowUsbDebugging(alwaysAllow, publicKey);
8584e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby        }
8594e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby    }
8604e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby
8614e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby    public void denyUsbDebugging() {
8624e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby        if (mDebuggingManager != null) {
8634e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby            mDebuggingManager.denyUsbDebugging();
8644e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby        }
8654e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby    }
8664e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby
86746d0adf8256a42416584765625852b6e48497c90Mike Lockwood    public void dump(FileDescriptor fd, PrintWriter pw) {
86802e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood        if (mHandler != null) {
86902e4569b5f126ca916a7473117ddd17d4f2ccbbbMike Lockwood            mHandler.dump(fd, pw);
87046d0adf8256a42416584765625852b6e48497c90Mike Lockwood        }
8714e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby        if (mDebuggingManager != null) {
8724e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby            mDebuggingManager.dump(fd, pw);
8734e68bd420b6cfdbeadb5e69aa6448665b2da762bBenoit Goby        }
87446d0adf8256a42416584765625852b6e48497c90Mike Lockwood    }
87546d0adf8256a42416584765625852b6e48497c90Mike Lockwood
87646d0adf8256a42416584765625852b6e48497c90Mike Lockwood    private native String[] nativeGetAccessoryStrings();
87746d0adf8256a42416584765625852b6e48497c90Mike Lockwood    private native ParcelFileDescriptor nativeOpenAccessory();
878d462ecf8f82076d21c85bdeeefbd16a1a91d2805Mike Lockwood    private native boolean nativeIsStartRequested();
879166b05e0ce913711d27ce1a116b3f1fd906822e4Mike Lockwood    private native int nativeGetAudioMode();
88046d0adf8256a42416584765625852b6e48497c90Mike Lockwood}
881