();
- if (icicle != null && icicle.containsKey(FREEZE_ADDRESSES)) {
- addDevices(icicle.getStringArray(FREEZE_ADDRESSES),
- icicle.getStringArray(FREEZE_TYPES),
- icicle.getIntArray(FREEZE_RSSI));
- if (icicle.containsKey(FREEZE_PIN)) {
- String savedPin = icicle.getString(FREEZE_PIN);
- String pinAddress = icicle.getString(FREEZE_PIN_ADDRESS);
- mPinDialog = showPinDialog(savedPin, pinAddress);
- }
- mDiscoverableStartTime = icicle.getLong(FREEZE_DISCOVERABLE_START);
- } else {
- mStartScan = true;
- }
- }
-
- private void initUI() {
- mBTToggle = (CheckBoxPreference) findPreference(BT_ENABLE);
- mBTVisibility = (CheckBoxPreference) findPreference(BT_VISIBILITY);
- mBTName = (EditTextPreference) findPreference(BT_NAME);
- mBTDeviceList = (ProgressCategory) findPreference("bt_device_list");
- mBTDeviceList.setOrderingAsAdded(false);
- mRes = getResources();
- if (mIsEnabled) {
- String name = mBluetooth.getName();
- if (name != null) {
- mBTName.setSummary(name);
- }
- }
- mBTVisibility.setEnabled(mIsEnabled);
- mBTName.setEnabled(mIsEnabled);
- STR_CONNECTED = mRes.getString(R.string.bluetooth_connected);
- STR_PAIRED = mRes.getString(R.string.bluetooth_paired);
- STR_PAIRED_NOT_NEARBY =
- mRes.getString(R.string.bluetooth_paired_not_nearby);
- STR_CONNECTING = mRes.getString(R.string.bluetooth_connecting);
- STR_PAIRING = mRes.getString(R.string.bluetooth_pairing);
- STR_NOT_CONNECTED = mRes.getString(R.string.bluetooth_not_connected);
- getListView().setOnCreateContextMenuListener(this);
- }
-
- private boolean initBluetoothAPI() {
- mIntentFilter =
- new IntentFilter(BluetoothIntent.REMOTE_DEVICE_CONNECTED_ACTION);
- mIntentFilter.addAction(BluetoothIntent.REMOTE_DEVICE_DISCONNECTED_ACTION);
- mIntentFilter.addAction(BluetoothIntent.BONDING_CREATED_ACTION);
- mIntentFilter.addAction(BluetoothIntent.ENABLED_ACTION);
- mIntentFilter.addAction(BluetoothIntent.DISABLED_ACTION);
- mIntentFilter.addAction(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION);
- mIntentFilter.addAction(BluetoothIntent.REMOTE_DEVICE_DISAPPEARED_ACTION);
- mIntentFilter.addAction(BluetoothIntent.REMOTE_NAME_UPDATED_ACTION);
- mIntentFilter.addAction(BluetoothIntent.PAIRING_REQUEST_ACTION);
- mIntentFilter.addAction(BluetoothIntent.HEADSET_STATE_CHANGED_ACTION);
- mIntentFilter.addAction(BluetoothIntent.DISCOVERY_COMPLETED_ACTION);
- mIntentFilter.addAction(BluetoothIntent.DISCOVERY_STARTED_ACTION);
- mIntentFilter.addAction(BluetoothIntent.MODE_CHANGED_ACTION);
-
- mBluetooth = (BluetoothDevice)getSystemService(BLUETOOTH_SERVICE);
- mBluetoothHeadset = new BluetoothHeadset(this);
- if (mBluetooth == null) { // If the environment doesn't support BT
- return false;
- }
- mIsEnabled = mBluetooth.isEnabled();
- return true;
- }
-
- @Override
- protected void onResume() {
- super.onResume();
-
- sIsRunning = true;
- mHandler.removeMessages(HANDLE_PAUSE_TIMEOUT);
- registerReceiver(mReceiver, mIntentFilter);
-
- mIsEnabled = mBluetooth.isEnabled();
- updateStatus();
- final boolean discoverable = mBluetooth.getMode() ==
- BluetoothDevice.MODE_DISCOVERABLE;
- mBTDeviceList.setProgress(mIsEnabled && mBluetooth.isDiscovering());
- mBTVisibility.setChecked(mIsEnabled && discoverable);
-
- if (discoverable) {
- mHandler.sendMessage(
- mHandler.obtainMessage(HANDLE_DISCOVERABLE_TIMEOUT));
- }
-
- if (mIsEnabled && mStartScan) {
- // First attempt after 100ms
- mHandler.sendMessageDelayed(
- mHandler.obtainMessage(HANDLE_INITIAL_SCAN, 1), 100);
- }
- mStartScan = false;
-
- // Check if headset status changed since we paused
- String connected = mBluetoothHeadset.getHeadsetAddress();
- if (connected != null) {
- updateRemoteDeviceStatus(connected);
- }
- if (mLastConnected != null) {
- updateRemoteDeviceStatus(mLastConnected);
- }
- }
-
- @Override
- protected void onPause() {
- sIsRunning = false;
-
- unregisterReceiver(mReceiver);
-
- // Wait for a few seconds and cleanup any pending requests, states
- mHandler.sendMessageDelayed(
- mHandler.obtainMessage(HANDLE_PAUSE_TIMEOUT,
- new Object[] { mBluetooth, mPinAddress }),
- PAUSE_TIMEOUT);
- super.onPause();
- }
-
- @Override
- protected void onDestroy() {
- mBluetoothHeadset.close();
- sDeviceCallback.setHandler(null);
-
- super.onDestroy();
- }
-
- @Override
- public void onConfigurationChanged(Configuration c) {
- super.onConfigurationChanged(c);
- // Don't do anything on keyboardHidden/orientation change, as we need
- // to make sure that we don't lose pairing request intents.
- }
-
- public static boolean isRunning() {
- return sIsRunning;
- }
-
- @Override
- protected void onSaveInstanceState(Bundle icicle) {
- int deviceCount = mBTDeviceList.getPreferenceCount();
- String [] addresses = new String[deviceCount];
- String [] states = new String[deviceCount];
- int [] weights = new int[deviceCount];
- for (int i = 0; i < deviceCount; i++) {
- BluetoothListItem p = (BluetoothListItem) mBTDeviceList.getPreference(i);
- CharSequence summary = p.getSummary();
- if (summary != null) {
- states[i] = summary.toString();
- } else {
- states[i] = STR_NOT_CONNECTED;
- }
- addresses[i] = getAddressFromKey(p.getKey());
- weights[i] = p.getWeight();
- }
- icicle.putStringArray(FREEZE_ADDRESSES, addresses);
- icicle.putStringArray(FREEZE_TYPES, states);
- icicle.putIntArray(FREEZE_RSSI, weights);
- icicle.putLong(FREEZE_DISCOVERABLE_START, mDiscoverableStartTime);
- if (mPinDialog != null && mPinDialog.isShowing()) {
- icicle.putString(FREEZE_PIN, mPinEdit.getText().toString());
- icicle.putString(FREEZE_PIN_ADDRESS, mPinAddress);
- }
- super.onSaveInstanceState(icicle);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- menu.add(0, MENU_SCAN_ID, 0,
- mRes.getString(R.string.bluetooth_scan_for_devices))
- .setIcon(R.drawable.ic_menu_scan_bluetooth);
- menu.add(0, MENU_CLEAR_ID, 0,
- mRes.getString(R.string.bluetooth_clear_list))
- .setIcon(android.R.drawable.ic_menu_close_clear_cancel);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case MENU_SCAN_ID:
- startScanning();
- return true;
- case MENU_CLEAR_ID:
- clearDevices();
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
-
- @Override
- public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
- if (!(menuInfo instanceof AdapterContextMenuInfo)) {
- return;
- }
- int position = ((AdapterContextMenuInfo)menuInfo).position;
- Preference pref = (Preference) getPreferenceScreen().getRootAdapter().getItem(position);
- if (!(pref instanceof BluetoothListItem)) {
- return;
- }
- String address = getAddressFromKey(pref.getKey());
- // Setup the menu header
- String name = mBluetooth.getRemoteName(address);
- menu.setHeaderTitle(name != null? name : address);
- int n = 0;
- if (mBluetoothHeadset.isConnected(address)) {
- menu.add(0, MENU_DISCONNECT, n++, R.string.bluetooth_disconnect);
- } else {
- menu.add(0, MENU_CONNECT, n++, R.string.bluetooth_connect);
- }
- if (mBluetooth.hasBonding(address)) {
- menu.add(0, MENU_UNPAIR, n++, R.string.bluetooth_unpair);
- } else {
- menu.add(0, MENU_PAIR, n++, R.string.bluetooth_pair);
- }
- }
-
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- AdapterView.AdapterContextMenuInfo info;
- if (!(item.getMenuInfo() instanceof AdapterContextMenuInfo)) {
- return false;
- }
- info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
- Preference pref = (Preference) getPreferenceScreen().getRootAdapter().
- getItem(info.position);
- String address = getAddressFromKey(pref.getKey());
- mBluetooth.cancelDiscovery();
- switch (item.getItemId()) {
- case MENU_DISCONNECT:
- if (mBluetoothHeadset.isConnected(address)) {
- mBluetoothHeadset.disconnectHeadset();
- }
- break;
- case MENU_CONNECT:
- if (!mBluetoothHeadset.isConnected(address)) {
- updateRemoteDeviceStatus(address, STR_CONNECTING);
- connect(pref, address);
- }
- break;
- case MENU_UNPAIR:
- if (mBluetooth.hasBonding(address)) {
- mBluetooth.removeBonding(address);
- updateRemoteDeviceStatus(address);
- }
- break;
- case MENU_PAIR:
- if (!mBluetooth.hasBonding(address)) {
- pair(pref, address);
- }
- break;
- }
- return true;
- }
-
- private void startScanning() {
- if (mIsEnabled && mBluetooth.isDiscovering()) {
- return;
- }
- resetDeviceListUI();
- if (mIsEnabled) {
- mBluetooth.startDiscovery();
- }
- }
-
- private void clearDevices() {
- String [] addresses = mBluetooth.listBondings();
- if (addresses != null) {
- for (int i = 0; i < addresses.length; i++) {
- unbond(addresses[i]);
- }
- }
- resetDeviceListUI();
- }
-
- /* Update the Bluetooth toggle and visibility summary */
- private void updateStatus() {
- boolean started = mIsEnabled;
- mBTToggle.setChecked(started);
- }
-
- private void updateRemoteDeviceStatus(String address) {
- if (address != null) {
- Preference device = mDeviceMap.get(address);
- if (device == null) {
- // This device is not in our discovered list
- // Let's add the device, if BT is not shut down already
- if (mIsEnabled) {
- addDeviceToUI(address, null, null, WEIGHT_PAIRED);
- }
- return;
- }
- device.setEnabled(true);
- if (address.equals(mBluetoothHeadset.getHeadsetAddress())) {
- int state = mBluetoothHeadset.getState();
- switch (state) {
- case BluetoothHeadset.STATE_CONNECTED:
- device.setSummary(STR_CONNECTED);
- mLastConnected = address;
- break;
- case BluetoothHeadset.STATE_CONNECTING:
- device.setSummary(STR_CONNECTING);
- break;
- case BluetoothHeadset.STATE_DISCONNECTED:
- if (mBluetooth.hasBonding(address)) {
- device.setSummary(STR_PAIRED);
- }
- break;
- }
- } else if (mBluetooth.hasBonding(address)) {
- device.setSummary(STR_PAIRED);
- } else {
- device.setSummary(STR_NOT_CONNECTED);
- }
- }
- }
-
- private void updateRemoteDeviceStatus(String address, String summary) {
- Preference device = mDeviceMap.get(address);
- if (device != null) {
- device.setEnabled(true);
- device.setSummary(summary);
- }
- }
-
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
- if (key.equals(BT_NAME)) {
- String name = sharedPreferences.getString(key, null);
- if (name == null) {
- return;
- }
- if (mBluetooth.setName(name)) {
- mBTName.setSummary(name);
- }
- }
- }
-
- private String getAddressFromKey(String key) {
- if (key != null) {
- return key.substring(BT_KEY_LENGTH);
- }
- return "";
- }
-
- private void sendPin(String pin) {
- byte[] pinBytes = BluetoothDevice.convertPinToBytes(pin);
- if (pinBytes == null) {
- mBluetooth.cancelPin(mPinAddress);
- } else {
- mBluetooth.setPin(mPinAddress, pinBytes);
- }
- mPinAddress = null;
- }
-
- private AlertDialog showPinDialog(String savedPin, String pinAddress) {
- if (mPinDialog != null) {
- return mPinDialog;
- }
- View view = LayoutInflater.from(this).inflate(
- R.layout.bluetooth_pin_entry, null);
- mPinEdit = (EditText) view.findViewById(R.id.text);
- mPinEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
- mPinEdit.setOnKeyListener(this);
- mPinAddress = pinAddress;
-
- if (savedPin != null) {
- mPinEdit.setText(savedPin);
- }
-
- String remoteName = mBluetooth.getRemoteName(mPinAddress);
- if (remoteName == null) {
- remoteName = mPinAddress;
- }
-
- AlertDialog ad = new AlertDialog.Builder(this)
- .setTitle(getString(R.string.bluetooth_notif_title))
- .setMessage(getString(R.string.bluetooth_enter_pin_msg) + remoteName)
- .setView(view)
- .setPositiveButton(android.R.string.ok, mDisconnectListener)
- .setNegativeButton(android.R.string.cancel, mDisconnectListener)
- .setOnCancelListener(mCancelListener)
- .show();
- ad.setCanceledOnTouchOutside(false);
- // Making an assumption here that the dialog buttons have the ids starting
- // with ...button1 as below
- mPinButton1 = ad.findViewById(com.android.internal.R.id.button1);
- if (mPinButton1 != null) {
- mPinButton1.setEnabled(savedPin != null? savedPin.length() > 0 : false);
- }
- return ad;
- }
-
- public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
- if (preference == mBTToggle) {
- toggleBT();
- return false;
- } else if (preference == mBTVisibility) {
- boolean vis = mBTVisibility.isChecked();
- if (!vis) {
- // Cancel discoverability
- mBluetooth.setMode(BluetoothDevice.MODE_CONNECTABLE);
- mHandler.removeMessages(HANDLE_DISCOVERABLE_TIMEOUT);
- } else {
- mBluetooth.setMode(BluetoothDevice.MODE_DISCOVERABLE);
- mBTVisibility.setSummaryOn(
- getResources().getString(R.string.bluetooth_is_discoverable,
- String.valueOf(mDiscoverableTime)));
- mDiscoverableStartTime = SystemClock.elapsedRealtime();
- mHandler.sendMessageDelayed(
- mHandler.obtainMessage(HANDLE_DISCOVERABLE_TIMEOUT), 1000);
- }
- } else {
- String key = preference.getKey();
- if (key.startsWith(BT_KEY_PREFIX)) {
- // Extract the device address from the key
- String address = getAddressFromKey(key);
- if (mBluetoothHeadset.isConnected(address)) {
- askDisconnect(address);
- } else if (mBluetooth.hasBonding(address)) {
- if (mIsEnabled) {
- mBluetooth.cancelDiscovery();
- }
- updateRemoteDeviceStatus(address, STR_CONNECTING);
- connect(preference, address);
- } else {
- if (mIsEnabled) {
- mBluetooth.cancelDiscovery();
- }
- pair(preference, address);
- }
- }
- }
- return false;
- }
-
- /* Handle the key input to the PIN entry dialog */
- public boolean onKey(View v, int keyCode, KeyEvent event) {
-
- if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
- || keyCode == KeyEvent.KEYCODE_ENTER) {
- String pin = ((EditText)v).getText().toString();
- if (pin != null && pin.length() > 0) {
- sendPin(pin);
- mPinDialog.dismiss();
- return true;
- }
- } else if (mPinButton1 != null) {
- boolean valid =
- BluetoothDevice.convertPinToBytes(((EditText)v).getText().toString()) != null;
- mPinButton1.setEnabled(valid);
- }
- return false;
- }
-
- private void askDisconnect(String address) {
- String name = mBluetooth.getRemoteName(address);
- if (name == null) {
- name = mRes.getString(R.string.bluetooth_device);
- }
- String message = mRes.getString(R.string.bluetooth_disconnect_blank, name);
-
- mDisconnectAddress = address;
-
- AlertDialog ad = new AlertDialog.Builder(this)
- .setTitle(message)
- .setPositiveButton(android.R.string.ok, mDisconnectListener)
- .setNegativeButton(android.R.string.cancel, null)
- .show();
- ad.setCanceledOnTouchOutside(false);
-
- }
-
- private void pairingDone(String address, boolean result) {
- Preference pref = mDeviceMap.get(address);
- if (pref != null) {
- pref.setEnabled(true);
- updateRemoteDeviceStatus(address);
- } else if (result) {
- // We've paired to a device that isn't in our list
- addDeviceToUI(address, STR_PAIRED, mBluetooth.getRemoteName(address),
- WEIGHT_PAIRED);
- }
- }
-
- private void pair(Preference pref, String address) {
- pref.setEnabled(false);
- pref.setSummary(STR_PAIRING);
- mBluetooth.createBonding(address, sDeviceCallback);
- }
-
- private void connect(Preference pref, String address) {
- pref.setEnabled(false);
- //TODO: Prompt the user to confirm they will disconnect current headset
- disconnect();
- mBluetoothHeadset.connectHeadset(address, mHeadsetCallback);
- }
-
- private void disconnect() {
- int state = mBluetoothHeadset.getState();
- if (state == BluetoothHeadset.STATE_CONNECTING ||
- state == BluetoothHeadset.STATE_CONNECTED) {
- mBluetoothHeadset.disconnectHeadset();
- }
- }
-
- private void toggleBT() {
- if (mIsEnabled) {
- mBTToggle.setSummaryOn(mRes.getString(R.string.bluetooth_stopping));
- mBTDeviceList.setProgress(false);
- // Force shutdown.
- mBluetooth.cancelDiscovery();
- mBluetooth.disable();
- } else {
- mBTToggle.setSummaryOff(mRes.getString(R.string.bluetooth_enabling));
- mBTToggle.setChecked(false);
- mBTToggle.setEnabled(false);
- if (!mBluetooth.enable()) {
- mBTToggle.setEnabled(true);
- }
- }
- }
-
- private void addDeviceToUI(String address, String summary, String name,
- int rssi) {
-
- if (address == null) {
- return;
- }
-
- BluetoothListItem p;
- if (mDeviceMap.containsKey(address)) {
- p = (BluetoothListItem) mDeviceMap.get(address);
- if (summary != null && summary.equals(STR_NOT_CONNECTED)) {
- if (mBluetooth.hasBonding(address)) {
- summary = STR_PAIRED;
- }
- }
- CharSequence oldSummary = p.getSummary();
- if (oldSummary != null && oldSummary.equals(STR_CONNECTED)) {
- summary = STR_CONNECTED; // Don't override connected with paired
- mLastConnected = address;
- }
- } else {
- p = new BluetoothListItem(this, null);
- }
- if (name == null) {
- name = mBluetooth.getRemoteName(address);
- }
- if (name == null) {
- name = address;
- }
-
- p.setTitle(name);
- p.setSummary(summary);
- p.setKey(BT_KEY_PREFIX + address);
- // Enable the headset icon if it is most probably a headset class device
- if (DeviceClass.getMajorClass(mBluetooth.getRemoteClass(address)) ==
- DeviceClass.MAJOR_CLASS_AUDIO_VIDEO) {
- p.setHeadset(true);
- }
- p.setWeight(rssi);
- if (!mDeviceMap.containsKey(address)) {
- mBTDeviceList.addPreference(p);
- mDeviceMap.put(address, p);
- }
- }
-
- private void addDevices(String [] addresses,
- String[] deviceStatus, int[] rssi) {
- for (int i = 0; i < addresses.length; i++) {
- String status = deviceStatus[i];
- String name = mBluetooth.getRemoteName(addresses[i]);
- String address = addresses[i];
- // Query the status if it's not known
- if (status == null) {
- if (mBluetoothHeadset.isConnected(addresses[i])) {
- status = STR_CONNECTED;
- mLastConnected = address;
- } else if (mBluetooth.hasBonding(addresses[i])) {
- status = STR_PAIRED;
- } else {
- status = STR_NOT_CONNECTED;
- }
- }
- addDeviceToUI(address, status, name, rssi[i]);
- }
- }
-
- private void removeDeviceFromUI(String address) {
- Preference p = mDeviceMap.get(address);
- if (p == null) {
- return;
- }
- mBTDeviceList.removePreference(p);
- mDeviceMap.remove(address);
- }
-
- private void updateDeviceName(String address, String name) {
- Preference p = mDeviceMap.get(address);
- if (p != null) {
- p.setTitle(name);
- }
- }
-
- private void resetDeviceListUI() {
- mDeviceMap.clear();
-
- while (mBTDeviceList.getPreferenceCount() > 0) {
- mBTDeviceList.removePreference(mBTDeviceList.getPreference(0));
- }
- if (!mIsEnabled) {
- return;
- }
-
- String connectedDevice = mBluetoothHeadset.getHeadsetAddress();
- if (connectedDevice != null && mBluetoothHeadset.isConnected(connectedDevice)) {
- addDeviceToUI(connectedDevice, STR_CONNECTED,
- mBluetooth.getRemoteName(connectedDevice), WEIGHT_CONNECTED);
- }
- String [] bondedDevices = mBluetooth.listBondings();
- if (bondedDevices != null) {
- for (int i = 0; i < bondedDevices.length; i++) {
- addDeviceToUI(bondedDevices[i], STR_PAIRED_NOT_NEARBY,
- mBluetooth.getRemoteName(bondedDevices[i]), WEIGHT_PAIRED);
- }
- }
- }
-
- private void unbond(String address) {
- mBluetooth.removeBonding(address);
- }
-
- private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- String address = intent.getStringExtra(BluetoothIntent.ADDRESS);
- if (action.equals(BluetoothIntent.ENABLED_ACTION)) {
- mIsEnabled = true;
- mBTToggle.setChecked(true);
- mBTToggle.setSummaryOn(mRes.getString(R.string.bluetooth_enabled));
- mBTToggle.setEnabled(true);
- String name = mBluetooth.getName();
- if (name != null) {
- mBTName.setSummary(name);
- }
- // save the "enabled" setting to database, so we can
- // remember it on startup.
- Settings.System.putInt(getContentResolver(),
- Settings.System.BLUETOOTH_ON, 1);
- resetDeviceListUI();
- if (mAutoDiscovery) {
- mBluetooth.startDiscovery();
- }
- } else if (action.equals(BluetoothIntent.DISABLED_ACTION)) {
- mIsEnabled = false;
- mBTToggle.setSummaryOff(mRes.getString(R.string.bluetooth_disabled));
- resetDeviceListUI();
- mBTVisibility.setChecked(false);
- // save the "disabled" setting to database
- Settings.System.putInt(getContentResolver(),
- Settings.System.BLUETOOTH_ON, 0);
- } else if (action.equals(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION)) {
- if (address != null) {
- int rssi = intent.getShortExtra(BluetoothIntent.RSSI,
- (short) WEIGHT_UNKNOWN);
- addDeviceToUI(address, STR_NOT_CONNECTED, null, rssi);
- }
- } else if (action.equals(BluetoothIntent.REMOTE_NAME_UPDATED_ACTION)) {
- String name = intent.getStringExtra(BluetoothIntent.NAME);
- updateDeviceName(address, name);
- } else if (action.equals(BluetoothIntent.REMOTE_DEVICE_DISAPPEARED_ACTION)) {
- removeDeviceFromUI(address);
- } else if (action.equals(BluetoothIntent.PAIRING_REQUEST_ACTION)) {
- mHandler.sendMessage(mHandler.obtainMessage(HANDLE_PIN_REQUEST, address));
- } else if (action.equals(BluetoothIntent.HEADSET_STATE_CHANGED_ACTION)) {
- int state = intent.getIntExtra(BluetoothIntent.HEADSET_STATE,
- BluetoothHeadset.STATE_ERROR);
- if (state == BluetoothHeadset.STATE_CONNECTED) {
- mHandler.sendMessage(mHandler.obtainMessage(HANDLE_CONNECTED, address));
- } else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
- mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISCONNECTED, address));
- } else if (state == BluetoothHeadset.STATE_CONNECTING) {
- mHandler.sendMessage(mHandler.obtainMessage(HANDLE_CONNECTING, address));
- }
- } else if (action.equals(BluetoothIntent.DISCOVERY_STARTED_ACTION)) {
- mBTDeviceList.setProgress(true);
- } else if (action.equals(BluetoothIntent.DISCOVERY_COMPLETED_ACTION)) {
- mBTDeviceList.setProgress(false);
- } else if (action.equals(BluetoothIntent.MODE_CHANGED_ACTION)) {
- mBTVisibility.setChecked(
- mBluetooth.getMode() == BluetoothDevice.MODE_DISCOVERABLE);
- } else if (action.equals(BluetoothIntent.BONDING_CREATED_ACTION)) {
- mHandler.sendMessage(mHandler.obtainMessage(HANDLE_PAIRING_PASSED, address));
- } else if (action.equals(BluetoothIntent.REMOTE_DEVICE_CONNECTED_ACTION)) {
- mHandler.sendMessage(mHandler.obtainMessage(HANDLE_CONNECTED, address));
- } else if (action.equals(BluetoothIntent.REMOTE_DEVICE_DISCONNECTED_ACTION)) {
- mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISCONNECTED, address));
- }
- }
- };
-
-
- static class DeviceCallback extends IBluetoothDeviceCallback.Stub {
- Handler messageHandler;
-
- public void setHandler(Handler handler) {
- synchronized (this) {
- messageHandler = handler;
- }
- }
-
- public void onCreateBondingResult(String address, int result) {
- synchronized (this) {
- if (messageHandler != null) {
- if (result == BluetoothDevice.RESULT_FAILURE) {
- messageHandler.sendMessage(messageHandler.obtainMessage(
- HANDLE_PAIRING_FAILED, address));
- } else {
- messageHandler.sendMessage(messageHandler.obtainMessage(
- HANDLE_PAIRING_PASSED, address));
- }
- }
- }
- }
-
- public void onEnableResult(int result) { }
- public void onGetRemoteServiceChannelResult(String address, int channel) { }
- };
-
- private IBluetoothHeadsetCallback mHeadsetCallback = new IBluetoothHeadsetCallback.Stub() {
- public void onConnectHeadsetResult(String address, int resultCode) {
- if (resultCode == BluetoothHeadset.RESULT_SUCCESS) {
- mHandler.sendMessage(mHandler.obtainMessage(HANDLE_CONNECTED, address));
- } else {
- // Make toast in UI thread
- mHandler.sendMessage(mHandler.obtainMessage(HANDLE_FAILED_TO_CONNECT, resultCode,
- -1, address));
- }
- }
- };
-
- private Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case HANDLE_CONNECTED:
- case HANDLE_DISCONNECTED:
- case HANDLE_CONNECTING:
- updateRemoteDeviceStatus((String) msg.obj);
- break;
- case HANDLE_FAILED_TO_CONNECT:
- updateRemoteDeviceStatus((String) msg.obj);
- String name = mBluetooth.getRemoteName((String) msg.obj);
- if (name == null) {
- name = (String) msg.obj;
- }
- if (msg.arg1 == BluetoothHeadset.RESULT_FAILURE) {
- Toast.makeText(BluetoothSettings.this,
- mRes.getString(R.string.failed_to_connect, name),
- Toast.LENGTH_SHORT).show();
- }
- break;
- case HANDLE_PIN_REQUEST:
- mPinDialog = showPinDialog(null, (String) msg.obj);
- break;
- case HANDLE_DISCOVERABLE_TIMEOUT:
- long nowTime = SystemClock.elapsedRealtime();
- int secondsLeft = mDiscoverableTime
- - (int) (nowTime - mDiscoverableStartTime) / 1000;
- if (secondsLeft > 0) {
- mBTVisibility.setSummaryOn(
- getResources().getString(R.string.bluetooth_is_discoverable,
- String.valueOf(secondsLeft)));
- sendMessageDelayed(obtainMessage(HANDLE_DISCOVERABLE_TIMEOUT), 1000);
- } else {
- mBluetooth.setMode(BluetoothDevice.MODE_CONNECTABLE);
- mBTVisibility.setChecked(false);
- }
- break;
- case HANDLE_INITIAL_SCAN:
- if (mBluetoothHeadset.getState() == BluetoothHeadset.STATE_ERROR &&
- ((Integer)msg.obj).intValue() < 2) {
- // Second attempt after another 100ms
- sendMessageDelayed(obtainMessage(HANDLE_INITIAL_SCAN, 2), 100);
- } else {
- resetDeviceListUI();
- if (mAutoDiscovery) {
- mBluetooth.cancelDiscovery();
- mBluetooth.startDiscovery();
- }
- }
- break;
- case HANDLE_PAIRING_PASSED:
- String addr = (String) msg.obj;
- pairingDone(addr, true);
- break;
- case HANDLE_PAIRING_FAILED:
- String address = (String) msg.obj;
- pairingDone(address, false);
- String pairName = mBluetooth.getRemoteName(address);
- if (pairName == null) {
- pairName = address;
- }
- Toast.makeText(BluetoothSettings.this,
- mRes.getString(R.string.failed_to_pair, pairName),
- Toast.LENGTH_SHORT).show();
- break;
- case HANDLE_PAUSE_TIMEOUT:
- // Possibility of race condition, but not really harmful
- if (!sIsRunning) {
- Object[] params = (Object[]) msg.obj;
- BluetoothDevice bluetooth = (BluetoothDevice) params[0];
- if (bluetooth.isEnabled()) {
- if (bluetooth.isDiscovering()) {
- bluetooth.cancelDiscovery();
- }
- if (params[1] != null) {
- bluetooth.cancelBondingProcess((String) params[1]);
- }
- bluetooth.setMode(BluetoothDevice.MODE_CONNECTABLE);
- }
- }
- break;
- }
- }
- };
-
- private DialogInterface.OnClickListener mDisconnectListener =
- new DialogInterface.OnClickListener() {
-
- public void onClick(DialogInterface dialog, int which) {
- if (dialog == mPinDialog) {
- if (which == DialogInterface.BUTTON1) {
- String pin = mPinEdit.getText().toString();
- if (pin != null && pin.length() > 0) {
- sendPin(pin);
- } else {
- sendPin(null);
- }
- } else {
- sendPin(null);
- }
- mPinDialog = null;
- mPinEdit = null;
- } else {
- if (which == DialogInterface.BUTTON1) {
- disconnect();
- }
- }
- }
- };
-
- private DialogInterface.OnCancelListener mCancelListener =
- new DialogInterface.OnCancelListener() {
- public void onCancel(DialogInterface dialog) {
- if (dialog == mPinDialog) {
- sendPin(null);
- }
- mPinDialog = null;
- mPinEdit = null;
- }
- };
-}
-
diff --git a/src/com/android/settings/ChooseLockPattern.java b/src/com/android/settings/ChooseLockPattern.java
index 973e655246c..3097e968327 100644
--- a/src/com/android/settings/ChooseLockPattern.java
+++ b/src/com/android/settings/ChooseLockPattern.java
@@ -45,6 +45,17 @@ import java.util.List;
*/
public class ChooseLockPattern extends Activity implements View.OnClickListener{
+ /**
+ * Used by the choose lock pattern wizard to indicate the wizard is
+ * finished, and each activity in the wizard should finish.
+ *
+ * Previously, each activity in the wizard would finish itself after
+ * starting the next activity. However, this leads to broken 'Back'
+ * behavior. So, now an activity does not finish itself until it gets this
+ * result.
+ */
+ static final int RESULT_FINISHED = RESULT_FIRST_USER;
+
// how long after a confirmation message is shown before moving on
static final int INFORMATION_MSG_TIMEOUT_MS = 3000;
@@ -298,6 +309,8 @@ public class ChooseLockPattern extends Activity implements View.OnClickListener{
mLockPatternView.clearPattern();
updateStage(Stage.Introduction);
} else if (mUiStage.leftMode == LeftButtonMode.Cancel) {
+ // They are canceling the entire wizard
+ setResult(RESULT_FINISHED);
finish();
} else {
throw new IllegalStateException("left footer button pressed, but stage of " +
@@ -368,6 +381,7 @@ public class ChooseLockPattern extends Activity implements View.OnClickListener{
}
if (resultCode != Activity.RESULT_OK) {
+ setResult(RESULT_FINISHED);
finish();
}
updateStage(Stage.Introduction);
@@ -475,6 +489,8 @@ public class ChooseLockPattern extends Activity implements View.OnClickListener{
mLockPatternUtils.setLockPatternEnabled(true);
mLockPatternUtils.setVisiblePatternEnabled(true);
}
+
+ setResult(RESULT_FINISHED);
finish();
}
}
diff --git a/src/com/android/settings/ChooseLockPatternExample.java b/src/com/android/settings/ChooseLockPatternExample.java
index 5feba4cbd3d..77517b9d3d4 100644
--- a/src/com/android/settings/ChooseLockPatternExample.java
+++ b/src/com/android/settings/ChooseLockPatternExample.java
@@ -25,6 +25,7 @@ import android.view.View;
import android.widget.ImageView;
public class ChooseLockPatternExample extends Activity implements View.OnClickListener {
+ private static final int REQUESTCODE_CHOOSE = 1;
private static final long START_DELAY = 1000;
protected static final String TAG = "Settings";
private View mNextButton;
@@ -59,15 +60,24 @@ public class ChooseLockPatternExample extends Activity implements View.OnClickLi
public void onClick(View v) {
if (v == mSkipButton) {
+ // Canceling, so finish all
+ setResult(ChooseLockPattern.RESULT_FINISHED);
finish();
} else if (v == mNextButton) {
stopAnimation(mAnimation);
Intent intent = new Intent(this, ChooseLockPattern.class);
- startActivity(intent);
+ startActivityForResult(intent, REQUESTCODE_CHOOSE);
+ }
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ if (requestCode == REQUESTCODE_CHOOSE && resultCode == ChooseLockPattern.RESULT_FINISHED) {
+ setResult(resultCode);
finish();
}
}
-
+
private void initViews() {
mNextButton = findViewById(R.id.next_button);
mNextButton.setOnClickListener(this);
diff --git a/src/com/android/settings/ChooseLockPatternTutorial.java b/src/com/android/settings/ChooseLockPatternTutorial.java
index 9687b55f10c..a0a878a0554 100644
--- a/src/com/android/settings/ChooseLockPatternTutorial.java
+++ b/src/com/android/settings/ChooseLockPatternTutorial.java
@@ -24,8 +24,10 @@ import android.os.Bundle;
import android.view.View;
public class ChooseLockPatternTutorial extends Activity implements View.OnClickListener {
- protected View mNextButton;
- protected View mSkipButton;
+ private static final int REQUESTCODE_EXAMPLE = 1;
+
+ private View mNextButton;
+ private View mSkipButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -52,11 +54,22 @@ public class ChooseLockPatternTutorial extends Activity implements View.OnClickL
public void onClick(View v) {
if (v == mSkipButton) {
+ // Canceling, so finish all
+ setResult(ChooseLockPattern.RESULT_FINISHED);
finish();
} else if (v == mNextButton) {
- startActivity(new Intent(this, ChooseLockPatternExample.class));
+ startActivityForResult(new Intent(this, ChooseLockPatternExample.class),
+ REQUESTCODE_EXAMPLE);
+ }
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ if (requestCode == REQUESTCODE_EXAMPLE && resultCode == ChooseLockPattern.RESULT_FINISHED) {
+ setResult(resultCode);
finish();
}
}
+
}
diff --git a/src/com/android/settings/DateTimeSettings.java b/src/com/android/settings/DateTimeSettings.java
index ce9dd8cb547..ead38d15796 100644
--- a/src/com/android/settings/DateTimeSettings.java
+++ b/src/com/android/settings/DateTimeSettings.java
@@ -27,7 +27,6 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.SystemClock;
-import android.pim.DateFormat;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
@@ -35,6 +34,7 @@ import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
+import android.text.format.DateFormat;
import android.widget.DatePicker;
import android.widget.TimePicker;
@@ -281,9 +281,7 @@ public class DateTimeSettings
/* Get & Set values from the system settings */
private boolean is24Hour() {
- String setting = Settings.System.getString(getContentResolver(),
- Settings.System.TIME_12_24);
- return HOURS_24.equals(setting);
+ return DateFormat.is24HourFormat(this);
}
private void set24Hour(boolean is24Hour) {
diff --git a/src/com/android/settings/DevelopmentSettings.java b/src/com/android/settings/DevelopmentSettings.java
index 02b852b6a08..155f085f5f5 100644
--- a/src/com/android/settings/DevelopmentSettings.java
+++ b/src/com/android/settings/DevelopmentSettings.java
@@ -16,6 +16,7 @@
package com.android.settings;
+import android.os.BatteryManager;
import android.os.Bundle;
import android.os.SystemProperties;
import android.preference.Preference;
@@ -32,9 +33,11 @@ public class DevelopmentSettings extends PreferenceActivity {
private static final String ENABLE_ADB = "enable_adb";
private static final String KEEP_SCREEN_ON = "keep_screen_on";
+ private static final String ALLOW_MOCK_LOCATION = "allow_mock_location";
private CheckBoxPreference mEnableAdb;
private CheckBoxPreference mKeepScreenOn;
+ private CheckBoxPreference mAllowMockLocation;
@Override
protected void onCreate(Bundle icicle) {
@@ -44,16 +47,19 @@ public class DevelopmentSettings extends PreferenceActivity {
mEnableAdb = (CheckBoxPreference) findPreference(ENABLE_ADB);
mKeepScreenOn = (CheckBoxPreference) findPreference(KEEP_SCREEN_ON);
+ mAllowMockLocation = (CheckBoxPreference) findPreference(ALLOW_MOCK_LOCATION);
}
@Override
protected void onResume() {
super.onResume();
- mEnableAdb.setChecked(Settings.System.getInt(getContentResolver(),
- Settings.System.ADB_ENABLED, 0) != 0);
+ mEnableAdb.setChecked(Settings.Secure.getInt(getContentResolver(),
+ Settings.Secure.ADB_ENABLED, 0) != 0);
mKeepScreenOn.setChecked(Settings.System.getInt(getContentResolver(),
Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0) != 0);
+ mAllowMockLocation.setChecked(Settings.Secure.getInt(getContentResolver(),
+ Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0);
}
@Override
@@ -66,11 +72,15 @@ public class DevelopmentSettings extends PreferenceActivity {
}
if (preference == mEnableAdb) {
- Settings.System.putInt(getContentResolver(), Settings.System.ADB_ENABLED,
+ Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED,
mEnableAdb.isChecked() ? 1 : 0);
} else if (preference == mKeepScreenOn) {
Settings.System.putInt(getContentResolver(), Settings.System.STAY_ON_WHILE_PLUGGED_IN,
- mKeepScreenOn.isChecked() ? 1 : 0);
+ mKeepScreenOn.isChecked() ?
+ (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB) : 0);
+ } else if (preference == mAllowMockLocation) {
+ Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION,
+ mAllowMockLocation.isChecked() ? 1 : 0);
}
return false;
diff --git a/src/com/android/settings/DeviceInfoSettings.java b/src/com/android/settings/DeviceInfoSettings.java
index 58fc91e9892..5d72afcacdf 100644
--- a/src/com/android/settings/DeviceInfoSettings.java
+++ b/src/com/android/settings/DeviceInfoSettings.java
@@ -46,6 +46,7 @@ public class DeviceInfoSettings extends PreferenceActivity {
private static final String KEY_TERMS = "terms";
private static final String KEY_LICENSE = "license";
private static final String KEY_COPYRIGHT = "copyright";
+ private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
@Override
protected void onCreate(Bundle icicle) {
@@ -56,7 +57,7 @@ public class DeviceInfoSettings extends PreferenceActivity {
setSummary("firmware_version", "ro.build.version.release");
setSummary("baseband_version", "gsm.version.baseband");
setSummary("device_model", "ro.product.model");
- setSummary("build_number", "ro.build.description");
+ setSummary("build_number", "ro.build.version.incremental");
findPreference("kernel_version").setSummary(getFormattedKernelVersion());
/*
@@ -74,6 +75,8 @@ public class DeviceInfoSettings extends PreferenceActivity {
Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_TEAM,
Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
+ Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_SYSTEM_UPDATE_SETTINGS,
+ Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
}
private void setSummary(String preference, String property) {
diff --git a/src/com/android/settings/InputMethodsSettings.java b/src/com/android/settings/InputMethodsSettings.java
new file mode 100644
index 00000000000..d38779d0aa6
--- /dev/null
+++ b/src/com/android/settings/InputMethodsSettings.java
@@ -0,0 +1,191 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.android.settings;
+
+import java.util.HashSet;
+import java.util.List;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.RemoteException;
+import android.os.SystemProperties;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceScreen;
+import android.preference.CheckBoxPreference;
+import android.provider.Settings;
+import android.text.TextUtils;
+import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodManager;
+
+/*
+ * Displays preferences for input methods.
+ */
+public class InputMethodsSettings extends PreferenceActivity {
+ private List mInputMethodProperties;
+
+ final TextUtils.SimpleStringSplitter mStringColonSplitter
+ = new TextUtils.SimpleStringSplitter(':');
+
+ private String mLastInputMethodId;
+ private String mLastTickedInputMethodId;
+
+ static public String getInputMethodIdFromKey(String key) {
+ return key;
+ }
+
+ @Override
+ protected void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+
+ addPreferencesFromResource(R.xml.input_methods_prefs);
+
+ InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+
+ mInputMethodProperties = imm.getInputMethodList();
+
+ mLastInputMethodId = Settings.Secure.getString(getContentResolver(),
+ Settings.Secure.DEFAULT_INPUT_METHOD);
+
+ int N = (mInputMethodProperties == null ? 0 : mInputMethodProperties
+ .size());
+ for (int i = 0; i < N; ++i) {
+ InputMethodInfo property = mInputMethodProperties.get(i);
+ String prefKey = property.getId();
+
+ CharSequence label = property.loadLabel(getPackageManager());
+
+ // Add a check box.
+ CheckBoxPreference chkbxPref = new CheckBoxPreference(this);
+ chkbxPref.setKey(prefKey);
+ chkbxPref.setTitle(label);
+ getPreferenceScreen().addPreference(chkbxPref);
+
+ // If setting activity is available, add a setting screen entry.
+ if (null != property.getSettingsActivity()) {
+ PreferenceScreen prefScreen = new PreferenceScreen(this, null);
+ prefScreen.setKey(property.getSettingsActivity());
+ // XXX TODO: handle localization properly.
+ prefScreen.setTitle(label + " settings");
+ getPreferenceScreen().addPreference(prefScreen);
+ }
+ }
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+
+ final HashSet enabled = new HashSet();
+ String enabledStr = Settings.Secure.getString(getContentResolver(),
+ Settings.Secure.ENABLED_INPUT_METHODS);
+ if (enabledStr != null) {
+ final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
+ splitter.setString(enabledStr);
+ while (splitter.hasNext()) {
+ enabled.add(splitter.next());
+ }
+ }
+
+ // Update the statuses of the Check Boxes.
+ int N = mInputMethodProperties.size();
+ for (int i = 0; i < N; ++i) {
+ final String id = mInputMethodProperties.get(i).getId();
+ CheckBoxPreference pref = (CheckBoxPreference) findPreference(mInputMethodProperties
+ .get(i).getId());
+ pref.setChecked(enabled.contains(id));
+ }
+ mLastTickedInputMethodId = null;
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+
+ StringBuilder builder = new StringBuilder(256);
+
+ boolean haveLastInputMethod = false;
+
+ int firstEnabled = -1;
+ int N = mInputMethodProperties.size();
+ for (int i = 0; i < N; ++i) {
+ final String id = mInputMethodProperties.get(i).getId();
+ CheckBoxPreference pref = (CheckBoxPreference) findPreference(id);
+ boolean hasIt = id.equals(mLastInputMethodId);
+ if (pref.isChecked()) {
+ if (builder.length() > 0) builder.append(':');
+ builder.append(id);
+ if (firstEnabled < 0) {
+ firstEnabled = i;
+ }
+ if (hasIt) haveLastInputMethod = true;
+ } else if (hasIt) {
+ mLastInputMethodId = mLastTickedInputMethodId;
+ }
+ }
+
+ // If the last input method is unset, set it as the first enabled one.
+ if (null == mLastInputMethodId || "".equals(mLastInputMethodId)) {
+ if (firstEnabled >= 0) {
+ mLastInputMethodId = mInputMethodProperties.get(firstEnabled).getId();
+ } else {
+ mLastInputMethodId = null;
+ }
+ }
+
+ Settings.Secure.putString(getContentResolver(),
+ Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
+ Settings.Secure.putString(getContentResolver(),
+ Settings.Secure.DEFAULT_INPUT_METHOD, mLastInputMethodId);
+ }
+
+ @Override
+ public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
+ Preference preference) {
+
+ // Those monkeys kept committing suicide, so we add this property
+ // to disable this functionality
+ if (!TextUtils.isEmpty(SystemProperties.get("ro.monkey"))) {
+ return false;
+ }
+
+ if (preference instanceof CheckBoxPreference) {
+ CheckBoxPreference chkPref = (CheckBoxPreference) preference;
+ String id = getInputMethodIdFromKey(chkPref.getKey());
+ if (chkPref.isChecked()) {
+ mLastTickedInputMethodId = id;
+ } else if (id.equals(mLastTickedInputMethodId)) {
+ mLastTickedInputMethodId = null;
+ }
+ } else if (preference instanceof PreferenceScreen) {
+ if (preference.getIntent() == null) {
+ PreferenceScreen pref = (PreferenceScreen) preference;
+ String activityName = pref.getKey();
+ String packageName = activityName.substring(0, activityName
+ .lastIndexOf("."));
+ if (activityName.length() > 0) {
+ Intent i = new Intent(Intent.ACTION_MAIN);
+ i.setClassName(packageName, activityName);
+ startActivity(i);
+ }
+ }
+ }
+
+ return false;
+ }
+}
diff --git a/src/com/android/settings/InstalledAppDetails.java b/src/com/android/settings/InstalledAppDetails.java
index 712f94db0e9..d3e73449d52 100644
--- a/src/com/android/settings/InstalledAppDetails.java
+++ b/src/com/android/settings/InstalledAppDetails.java
@@ -29,7 +29,6 @@ import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageDataObserver;
import android.content.pm.IPackageStatsObserver;
-import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.content.pm.PackageManager.NameNotFoundException;
@@ -37,6 +36,7 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
+import android.text.format.Formatter;
import android.util.Config;
import android.util.Log;
import java.util.ArrayList;
@@ -50,33 +50,62 @@ import android.widget.LinearLayout;
import android.widget.TextView;
/**
- * Activity to display application information from Settings
- *
+ * Activity to display application information from Settings. This activity presents
+ * extended information associated with a package like code, data, total size, permissions
+ * used by the application and also the set of default launchable activities.
+ * For system applications, an option to clear user data is displayed only if data size is > 0.
+ * System applications that do not want clear user data do not have this option.
+ * For non-system applications, there is no option to clear data. Instead there is an option to
+ * uninstall the application.
*/
public class InstalledAppDetails extends Activity implements View.OnClickListener, DialogInterface.OnClickListener {
private static final String TAG="InstalledAppDetails";
private static final int _UNKNOWN_APP=R.string.unknown;
- //wait times used for the async package manager api
private ApplicationInfo mAppInfo;
- private Button mUninstallButton;
+ private Button mAppButton;
private Button mActivitiesButton;
- private boolean mSysPackage;
- private boolean localLOGV=Config.LOGV || true;
+ private boolean mCanUninstall;
+ private boolean localLOGV=Config.LOGV || false;
private TextView mTotalSize;
private TextView mAppSize;
private TextView mDataSize;
+ private PkgSizeObserver mSizeObserver;
+ private ClearUserDataObserver mClearDataObserver;
+ // Views related to cache info
+ private View mCachePanel;
+ private TextView mCacheSize;
+ private Button mClearCacheButton;
+ private ClearCacheObserver mClearCacheObserver;
+
PackageStats mSizeInfo;
private Button mManageSpaceButton;
private PackageManager mPm;
- private String mBStr, mKbStr, mMbStr;
//internal constants used in Handler
- private static final int CLEAR_USER_DATA = 1;
private static final int OP_SUCCESSFUL = 1;
private static final int OP_FAILED = 2;
+ private static final int CLEAR_USER_DATA = 1;
private static final int GET_PKG_SIZE = 2;
+ private static final int CLEAR_CACHE = 3;
private static final String ATTR_PACKAGE_STATS="PackageStats";
+ // invalid size value used initially and also when size retrieval through PackageManager
+ // fails for whatever reason
+ private static final int SIZE_INVALID = -1;
+
+ // Resource strings
+ private CharSequence mInvalidSizeStr;
+ private CharSequence mComputingStr;
+ private CharSequence mAppButtonText;
+
+ // Possible btn states
+ private enum AppButtonStates {
+ CLEAR_DATA,
+ UNINSTALL,
+ NONE
+ }
+ private AppButtonStates mAppButtonState;
+
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
@@ -86,17 +115,22 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
case GET_PKG_SIZE:
refreshSizeInfo(msg);
break;
+ case CLEAR_CACHE:
+ // Refresh size info
+ mPm.getPackageSizeInfo(mAppInfo.packageName, mSizeObserver);
+ break;
default:
break;
}
}
};
- private boolean isSystemPackage() {
- if ((mAppInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
- return true;
+ private boolean isUninstallable() {
+ if (((mAppInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0) &&
+ ((mAppInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0)) {
+ return false;
}
- return false;
+ return true;
}
class ClearUserDataObserver extends IPackageDataObserver.Stub {
@@ -119,28 +153,46 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
}
}
+ class ClearCacheObserver extends IPackageDataObserver.Stub {
+ public void onRemoveCompleted(final String packageName, final boolean succeeded) {
+ final Message msg = mHandler.obtainMessage(CLEAR_CACHE);
+ msg.arg1 = succeeded?OP_SUCCESSFUL:OP_FAILED;
+ mHandler.sendMessage(msg);
+ }
+ }
+
private String getSizeStr(long size) {
- String retStr = "";
- if(size < 1024) {
- return String.valueOf(size)+mBStr;
+ if (size == SIZE_INVALID) {
+ return mInvalidSizeStr.toString();
}
- long kb, mb, rem;
- kb = size >> 10;
- rem = size - (kb << 10);
- if(kb < 1024) {
- if(rem > 512) {
- kb++;
+ return Formatter.formatFileSize(this, size);
+ }
+
+ private void setAppBtnState() {
+ boolean visible = false;
+ if(mCanUninstall) {
+ //app can clear user data
+ if((mAppInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA)
+ == ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) {
+ mAppButtonText = getText(R.string.clear_user_data_text);
+ mAppButtonState = AppButtonStates.CLEAR_DATA;
+ visible = true;
+ } else {
+ //hide button if diableClearUserData is set
+ visible = false;
+ mAppButtonState = AppButtonStates.NONE;
}
- retStr += String.valueOf(kb)+mKbStr;
- return retStr;
+ } else {
+ visible = true;
+ mAppButtonState = AppButtonStates.UNINSTALL;
+ mAppButtonText = getText(R.string.uninstall_text);
+ }
+ if(visible) {
+ mAppButton.setText(mAppButtonText);
+ mAppButton.setVisibility(View.VISIBLE);
+ } else {
+ mAppButton.setVisibility(View.GONE);
}
- mb = kb >> 10;
- if(kb >= 512) {
- //round off
- mb++;
- }
- retStr += String.valueOf(mb)+ mMbStr;
- return retStr;
}
/** Called when the activity is first created. */
@@ -152,45 +204,25 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
//get application's name from intent
Intent intent = getIntent();
final String packageName = intent.getStringExtra(ManageApplications.APP_PKG_NAME);
- mSizeInfo = intent.getParcelableExtra(ManageApplications.APP_PKG_SIZE);
- long total = -1;
- long code = -1;
- long data = -1;
- if(mSizeInfo != null) {
- total = mSizeInfo.cacheSize+mSizeInfo.codeSize+mSizeInfo.dataSize;
- code = mSizeInfo.codeSize;
- data = mSizeInfo.dataSize+mSizeInfo.cacheSize;
- }
- String unknownStr = getString(_UNKNOWN_APP);
- mBStr = getString(R.string.b_text);
- mKbStr = getString(R.string.kb_text);
- mMbStr = getString(R.string.mb_text);
- String totalSizeStr = unknownStr;
- if(total != -1) {
- totalSizeStr = getSizeStr(total);
- }
- String appSizeStr = unknownStr;
- if(code != -1) {
- appSizeStr = getSizeStr(code);
- }
- String dataSizeStr = unknownStr;
- if(data != -1) {
- dataSizeStr = getSizeStr(data);
- }
- if(localLOGV) Log.i(TAG, "packageName:"+packageName+", total="+total+
- "code="+code+", data="+data);
+ mComputingStr = getText(R.string.computing_size);
+ // Try retrieving package stats again
+ CharSequence totalSizeStr, appSizeStr, dataSizeStr;
+ totalSizeStr = appSizeStr = dataSizeStr = mComputingStr;
+ if(localLOGV) Log.i(TAG, "Have to compute package sizes");
+ mSizeObserver = new PkgSizeObserver();
+ mPm.getPackageSizeInfo(packageName, mSizeObserver);
+
try {
- mAppInfo = mPm.getApplicationInfo(packageName, 0);
+ mAppInfo = mPm.getApplicationInfo(packageName,
+ PackageManager.GET_UNINSTALLED_PACKAGES);
} catch (NameNotFoundException e) {
- Throwable th = e.fillInStackTrace();
Log.e(TAG, "Exception when retrieving package:"+packageName, e);
displayErrorDialog(R.string.app_not_found_dlg_text, true, true);
}
- setContentView(R.layout.installed_app_details);
- ((ImageView)findViewById(R.id.app_icon)).setImageDrawable(mPm.
- getApplicationIcon(mAppInfo));
+ setContentView(R.layout.installed_app_details);
+ ((ImageView)findViewById(R.id.app_icon)).setImageDrawable(mAppInfo.loadIcon(mPm));
//set application name TODO version
- CharSequence appName = mPm.getApplicationLabel(mAppInfo);
+ CharSequence appName = mAppInfo.loadLabel(mPm);
if(appName == null) {
appName = getString(_UNKNOWN_APP);
}
@@ -208,33 +240,22 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
mDataSize = (TextView)findViewById(R.id.data_size_text);
mDataSize.setText(dataSizeStr);
- mUninstallButton = ((Button)findViewById(R.id.uninstall_button));
+ mAppButton = ((Button)findViewById(R.id.uninstall_button));
//determine if app is a system app
- mSysPackage = isSystemPackage();
- if(localLOGV) Log.i(TAG, "Is systemPackage "+mSysPackage);
- int btnText;
- boolean btnClickable = true;
-
- if(mSysPackage) {
- //app can clear user data
- if((mAppInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA)
- == ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) {
- mUninstallButton.setText(R.string.clear_user_data_text);
- //disable button if data is 0
- if(data == 0) {
- mUninstallButton.setEnabled(false);
- } else {
- //enable button
- mUninstallButton.setOnClickListener(this);
- }
- } else {
- //hide button if diableClearUserData is set
- mUninstallButton.setVisibility(View.GONE);
- }
- } else {
- mUninstallButton.setText(R.string.uninstall_text);
- mUninstallButton.setOnClickListener(this);
+ mCanUninstall = !isUninstallable();
+ if(localLOGV) Log.i(TAG, "Is systemPackage "+mCanUninstall);
+ setAppBtnState();
+ mManageSpaceButton = (Button)findViewById(R.id.manage_space_button);
+ if(mAppInfo.manageSpaceActivityName != null) {
+ mManageSpaceButton.setVisibility(View.VISIBLE);
+ mManageSpaceButton.setOnClickListener(this);
}
+
+ // Cache section
+ mCachePanel = findViewById(R.id.cache_panel);
+ mCacheSize = (TextView) findViewById(R.id.cache_size_text);
+ mClearCacheButton = (Button) findViewById(R.id.clear_cache_button);
+
//clear activities
mActivitiesButton = (Button)findViewById(R.id.clear_activities_button);
List prefActList = new ArrayList();
@@ -251,23 +272,19 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
autoLaunchView.setText(R.string.auto_launch_enable_text);
mActivitiesButton.setOnClickListener(this);
}
- mManageSpaceButton = (Button)findViewById(R.id.manage_space_button);
- if(mAppInfo.manageSpaceActivityName != null) {
- mManageSpaceButton.setVisibility(View.VISIBLE);
- mManageSpaceButton.setOnClickListener(this);
+
+ // security permissions section
+ LinearLayout permsView = (LinearLayout) findViewById(R.id.permissions_section);
+ AppSecurityPermissions asp = new AppSecurityPermissions(this, packageName);
+ if(asp.getPermissionCount() > 0) {
+ permsView.setVisibility(View.VISIBLE);
+ // Make the security sections header visible
+ LinearLayout securityList = (LinearLayout) permsView.findViewById(
+ R.id.security_settings_list);
+ securityList.addView(asp.getPermissionsView());
+ } else {
+ permsView.setVisibility(View.GONE);
}
- //security permissions section
- AppSecurityPermissions asp = new AppSecurityPermissions(this);
- PackageInfo pkgInfo;
- try {
- pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
- } catch (NameNotFoundException e) {
- Log.w(TAG, "Couldnt retrieve permissions for package:"+packageName);
- return;
- }
- asp.setSecurityPermissionsView(pkgInfo);
- LinearLayout securityList = (LinearLayout) findViewById(R.id.security_settings_list);
- securityList.addView(asp.getPermissionsView());
}
private void displayErrorDialog(int msgId, final boolean finish, final boolean changed) {
@@ -292,7 +309,7 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
Intent intent = new Intent();
intent.putExtra(ManageApplications.APP_CHG, appChanged);
setResult(ManageApplications.RESULT_OK, intent);
- mUninstallButton.setEnabled(false);
+ mAppButton.setEnabled(false);
if(finish) {
finish();
}
@@ -305,29 +322,53 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
*/
private void refreshSizeInfo(Message msg) {
boolean changed = false;
- Intent intent = new Intent();
PackageStats newPs = msg.getData().getParcelable(ATTR_PACKAGE_STATS);
long newTot = newPs.cacheSize+newPs.codeSize+newPs.dataSize;
- long oldTot = mSizeInfo.cacheSize+mSizeInfo.codeSize+mSizeInfo.dataSize;
- if(newTot != oldTot) {
- mTotalSize.setText(getSizeStr(newTot));
- changed = true;
- }
- if(newPs.codeSize != mSizeInfo.codeSize) {
- mAppSize.setText(getSizeStr(newPs.codeSize));
- changed = true;
- }
- if((newPs.dataSize != mSizeInfo.dataSize) || (newPs.cacheSize != mSizeInfo.cacheSize)) {
- mDataSize.setText(getSizeStr(newPs.dataSize+newPs.cacheSize));
- changed = true;
- }
- if(changed) {
- mUninstallButton.setText(R.string.clear_user_data_text);
+ if(mSizeInfo == null) {
mSizeInfo = newPs;
- intent.putExtra(ManageApplications.APP_PKG_SIZE, mSizeInfo);
+ mTotalSize.setText(getSizeStr(newTot));
+ mAppSize.setText(getSizeStr(newPs.codeSize));
+ mDataSize.setText(getSizeStr(newPs.dataSize+newPs.cacheSize));
+ } else {
+ long oldTot = mSizeInfo.cacheSize+mSizeInfo.codeSize+mSizeInfo.dataSize;
+ if(newTot != oldTot) {
+ mTotalSize.setText(getSizeStr(newTot));
+ changed = true;
+ }
+ if(newPs.codeSize != mSizeInfo.codeSize) {
+ mAppSize.setText(getSizeStr(newPs.codeSize));
+ changed = true;
+ }
+ if((newPs.dataSize != mSizeInfo.dataSize) || (newPs.cacheSize != mSizeInfo.cacheSize)) {
+ mDataSize.setText(getSizeStr(newPs.dataSize+newPs.cacheSize));
+ }
+ if(changed) {
+ mSizeInfo = newPs;
+ }
+ }
+
+ long data = mSizeInfo.dataSize+mSizeInfo.cacheSize;
+ // Disable button if data is 0
+ if(mAppButtonState != AppButtonStates.NONE){
+ mAppButton.setText(mAppButtonText);
+ if((mAppButtonState == AppButtonStates.CLEAR_DATA) && (data == 0)) {
+ mAppButton.setEnabled(false);
+ } else {
+ mAppButton.setEnabled(true);
+ mAppButton.setOnClickListener(this);
+ }
+ }
+ refreshCacheInfo(newPs.cacheSize);
+ }
+
+ private void refreshCacheInfo(long cacheSize) {
+ // Set cache info
+ mCacheSize.setText(getSizeStr(cacheSize));
+ if (cacheSize <= 0) {
+ mClearCacheButton.setEnabled(false);
+ } else {
+ mClearCacheButton.setOnClickListener(this);
}
- intent.putExtra(ManageApplications.APP_CHG, changed);
- setResult(ManageApplications.RESULT_OK, intent);
}
/*
@@ -339,11 +380,10 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
String packageName = mAppInfo.packageName;
if(result == OP_SUCCESSFUL) {
Log.i(TAG, "Cleared user data for system package:"+packageName);
- PkgSizeObserver observer = new PkgSizeObserver();
- mPm.getPackageSizeInfo(packageName, observer);
+ mPm.getPackageSizeInfo(packageName, mSizeObserver);
} else {
- mUninstallButton.setText(R.string.clear_user_data_text);
- mUninstallButton.setEnabled(true);
+ mAppButton.setText(R.string.clear_user_data_text);
+ mAppButton.setEnabled(true);
}
}
@@ -352,20 +392,21 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
* button for a system package
*/
private void initiateClearUserDataForSysPkg() {
- mUninstallButton.setEnabled(false);
+ mAppButton.setEnabled(false);
//invoke uninstall or clear user data based on sysPackage
- boolean recomputeSizes = false;
String packageName = mAppInfo.packageName;
Log.i(TAG, "Clearing user data for system package");
- ClearUserDataObserver observer = new ClearUserDataObserver();
+ if(mClearDataObserver == null) {
+ mClearDataObserver = new ClearUserDataObserver();
+ }
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
- boolean res = am.clearApplicationUserData(packageName, observer);
+ boolean res = am.clearApplicationUserData(packageName, mClearDataObserver);
if(!res) {
//doesnt initiate clear. some error. should not happen but just log error for now
Log.i(TAG, "Couldnt clear application user data for package:"+packageName);
displayErrorDialog(R.string.clear_data_failed, false, false);
} else {
- mUninstallButton.setText(R.string.recompute_size);
+ mAppButton.setText(R.string.recompute_size);
}
}
@@ -375,8 +416,8 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
*/
public void onClick(View v) {
String packageName = mAppInfo.packageName;
- if(v == mUninstallButton) {
- if(mSysPackage) {
+ if(v == mAppButton) {
+ if(mCanUninstall) {
//display confirmation dialog
new AlertDialog.Builder(this)
.setTitle(getString(R.string.clear_data_dlg_title))
@@ -399,11 +440,17 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
Intent intent = new Intent(Intent.ACTION_DEFAULT);
intent.setClassName(mAppInfo.packageName, mAppInfo.manageSpaceActivityName);
startActivityForResult(intent, -1);
+ } else if (v == mClearCacheButton) {
+ // Lazy initialization of observer
+ if (mClearCacheObserver == null) {
+ mClearCacheObserver = new ClearCacheObserver();
+ }
+ mPm.deleteApplicationCacheFiles(packageName, mClearCacheObserver);
}
}
public void onClick(DialogInterface dialog, int which) {
- if(which == AlertDialog.BUTTON1) {
+ if(which == AlertDialog.BUTTON_POSITIVE) {
//invoke uninstall or clear user data based on sysPackage
initiateClearUserDataForSysPkg();
} else {
diff --git a/src/com/android/settings/LocalePicker.java b/src/com/android/settings/LocalePicker.java
index 46d9b52b68f..9ee82601be7 100644
--- a/src/com/android/settings/LocalePicker.java
+++ b/src/com/android/settings/LocalePicker.java
@@ -63,30 +63,55 @@ public class LocalePicker extends ListActivity {
setContentView(getContentView());
String[] locales = getAssets().getLocales();
- final int N = locales.length;
- mLocales = new Loc[N];
- for (int i = 0; i < N; i++) {
- Locale locale = null;
+ Arrays.sort(locales);
+
+ final int origSize = locales.length;
+ Loc[] preprocess = new Loc[origSize];
+ int finalSize = 0;
+ for (int i = 0 ; i < origSize; i++ ) {
String s = locales[i];
int len = s.length();
- if (len == 0) {
- locale = new Locale("en", "US");
- } else if (len == 2) {
- locale = new Locale(s);
+ if (len == 2) {
+ Locale l = new Locale(s);
+ preprocess[finalSize++] = new Loc(l.getDisplayLanguage(), l);
} else if (len == 5) {
- locale = new Locale(s.substring(0, 2), s.substring(3, 5));
- }
- String displayName = "";
- if (locale != null) {
- displayName = locale.getDisplayName();
- }
- if ("zz_ZZ".equals(s)) {
- displayName = "Pseudo...";
- }
+ String language = s.substring(0, 2);
+ String country = s.substring(3, 5);
+ Locale l = new Locale(language, country);
- mLocales[i] = new Loc(displayName, locale);
+ if (finalSize == 0) {
+ preprocess[finalSize++] = new Loc(l.getDisplayLanguage(), l);
+ } else {
+ // check previous entry:
+ // same lang and no country -> overwrite it with a lang-only name
+ // same lang and a country -> upgrade to full name and
+ // insert ours with full name
+ // diff lang -> insert ours with lang-only name
+ if (preprocess[finalSize-1].locale.getLanguage().equals(language)) {
+ String prevCountry = preprocess[finalSize-1].locale.getCountry();
+ if (prevCountry.length() == 0) {
+ preprocess[finalSize-1].locale = l;
+ preprocess[finalSize-1].label = l.getDisplayLanguage();
+ } else {
+ preprocess[finalSize-1].label = preprocess[finalSize-1].locale.getDisplayName();
+ preprocess[finalSize++] = new Loc(l.getDisplayName(), l);
+ }
+ } else {
+ String displayName;
+ if (s.equals("zz_ZZ")) {
+ displayName = "Pseudo...";
+ } else {
+ displayName = l.getDisplayLanguage();
+ }
+ preprocess[finalSize++] = new Loc(displayName, l);
+ }
+ }
+ }
+ }
+ mLocales = new Loc[finalSize];
+ for (int i = 0; i < finalSize ; i++) {
+ mLocales[i] = preprocess[i];
}
-
int layoutId = R.layout.locale_picker_item;
int fieldId = R.id.locale;
ArrayAdapter adapter = new ArrayAdapter(this, layoutId, fieldId, mLocales);
@@ -107,25 +132,11 @@ public class LocalePicker extends ListActivity {
Loc loc = mLocales[position];
config.locale = loc.locale;
- final String language = loc.locale.getLanguage();
- final String region = loc.locale.getCountry();
+
+ // indicate this isn't some passing default - the user wants this remembered
+ config.userSetLocale = true;
am.updateConfiguration(config);
-
- // Update the System properties
- SystemProperties.set("user.language", language);
- SystemProperties.set("user.region", region);
- // Write to file for persistence across reboots
- try {
- BufferedWriter bw = new BufferedWriter(new java.io.FileWriter(
- System.getenv("ANDROID_DATA") + "/locale"));
- bw.write(language + "_" + region);
- bw.close();
- } catch (java.io.IOException ioe) {
- Log.e(TAG,
- "Unable to persist locale. Error writing to locale file."
- + ioe);
- }
} catch (RemoteException e) {
// Intentionally left blank
}
diff --git a/src/com/android/settings/ManageApplications.java b/src/com/android/settings/ManageApplications.java
index 8389502518f..f1550f9780f 100644
--- a/src/com/android/settings/ManageApplications.java
+++ b/src/com/android/settings/ManageApplications.java
@@ -18,8 +18,12 @@ package com.android.settings;
import com.android.settings.R;
import android.app.Activity;
+import android.app.ActivityManager;
+import android.app.ListActivity;
+import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
+import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
@@ -27,320 +31,954 @@ import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
+import android.text.format.Formatter;
import android.util.Config;
import android.util.Log;
+import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
+import android.view.ViewGroup;
+import android.view.Window;
import android.widget.AdapterView;
+import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
-import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.TreeMap;
/**
* Activity to pick an application that will be used to display installation information and
- * options to upgrade/uninstall/delete user data for system applications.
+ * options to uninstall/delete user data for system applications. This activity
+ * can be launched through Settings or via the ACTION_MANAGE_PACKAGE_STORAGE
+ * intent.
* Initially a compute in progress message is displayed while the application retrieves
- * the size information of installed packages which is done asynchronously through a
- * handler. Once the computation is done package resource information is retrieved
- * and then the information is displayed on the screen. All
- * messages are passed through a Handler object.
- * Known issue: There could be some ordering issues when installing/uninstalling
- * applications when the application list is being scanned.
+ * the list of application information from the PackageManager. The size information
+ * for each package is refreshed to the screen. The resource(app description and
+ * icon) information for each package is not available yet, so some default values for size
+ * icon and descriptions are used initially. Later the resource information for each
+ * application is retrieved and dynamically updated on the screen.
+ * A Broadcast receiver registers for package additions or deletions when the activity is
+ * in focus. If the user installs or deletes packages when the activity has focus, the receiver
+ * gets notified and proceeds to add/delete these packages from the list on the screen.
+ * This is an unlikely scenario but could happen. The entire list gets created every time
+ * the activity's onStart gets invoked. This is to avoid having the receiver for the entire
+ * life cycle of the application.
+ * The applications can be sorted either alphabetically or
+ * based on size(descending). If this activity gets launched under low memory
+ * situations(A low memory notification dispatches intent
+ * ACTION_MANAGE_PACKAGE_STORAGE) the list is sorted per size.
+ * If the user selects an application, extended info(like size, uninstall/clear data options,
+ * permissions info etc.,) is displayed via the InstalledAppDetails activity.
+ * This activity passes the package name and size information to the
+ * InstalledAppDetailsActivity to avoid recomputation of the package size information.
*/
-public class ManageApplications extends Activity implements SimpleAdapter.ViewBinder, OnItemClickListener {
+public class ManageApplications extends ListActivity implements
+ OnItemClickListener, DialogInterface.OnCancelListener {
+ // TAG for this activity
private static final String TAG = "ManageApplications";
- //Application prefix information
- public static final String APP_PKG_PREFIX="com.android.settings.";
- public static final String APP_PKG_NAME=APP_PKG_PREFIX+"ApplicationPkgName";
- public static final String APP_PKG_SIZE= APP_PKG_PREFIX+"size";
- public static final String APP_CHG=APP_PKG_PREFIX+"changed";
- //constant value that can be used to check return code from sub activity.
- private static final int INSTALLED_APP_DETAILS = 1;
- //application attributes passed to sub activity that displays more app info
- private static final String KEY_APP_NAME = "ApplicationName";
- private static final String KEY_APP_ICON = "ApplicationIcon";
- private static final String KEY_APP_DESC = "ApplicationDescription";
- private static final String KEY_APP_SIZE= "ApplicationSize";
- //sort order that can be changed through the menu
- public static final int SORT_ORDER_ALPHA = 0;
- public static final int SORT_ORDER_SIZE = 1;
- //key and resource values used in constructing map for SimpleAdapter
- private static final String sKeys[] = new String[] { KEY_APP_NAME, KEY_APP_ICON,
- KEY_APP_DESC, KEY_APP_SIZE};
- private static final int sResourceIds[] = new int[] { R.id.app_name, R.id.app_icon,
- R.id.app_description, R.id.app_size};
- //List of ApplicationInfo objects for various applications
- private List mAppList;
- //SimpleAdapter used for managing items in the list
- private SimpleAdapter mAppAdapter;
- //map used to store size information which is used for displaying size information
- //in this activity as well as the subactivity. this is to avoid invoking package manager
- //api to retrieve size information
- private HashMap mSizeMap;
- private HashMap > mAppAdapterMap;
- //sort order
- private int mSortOrder = SORT_ORDER_ALPHA;
- //log information boolean
+ // log information boolean
private boolean localLOGV = Config.LOGV || false;
- private ApplicationInfo mCurrentPkg;
- private int mCurrentPkgIdx = 0;
- private static final int COMPUTE_PKG_SIZE_START = 1;
- private static final int COMPUTE_PKG_SIZE_DONE = 2;
- private static final int REMOVE_PKG=3;
- private static final int REORDER_LIST=4;
- private static final int ADD_PKG=5;
- private static final String ATTR_APP_IDX="ApplicationIndex";
- private static final String ATTR_CHAINED="Chained";
+
+ // attributes used as keys when passing values to InstalledAppDetails activity
+ public static final String APP_PKG_PREFIX = "com.android.settings.";
+ public static final String APP_PKG_NAME = APP_PKG_PREFIX+"ApplicationPkgName";
+ public static final String APP_PKG_SIZE = APP_PKG_PREFIX+"size";
+ public static final String APP_CHG = APP_PKG_PREFIX+"changed";
+
+ // attribute name used in receiver for tagging names of added/deleted packages
private static final String ATTR_PKG_NAME="PackageName";
+ private static final String ATTR_APP_PKG_STATS="ApplicationPackageStats";
+
+ // constant value that can be used to check return code from sub activity.
+ private static final int INSTALLED_APP_DETAILS = 1;
+
+ // sort order that can be changed through the menu can be sorted alphabetically
+ // or size(descending)
+ private static final int MENU_OPTIONS_BASE = 0;
+ public static final int SORT_ORDER_ALPHA = MENU_OPTIONS_BASE + 0;
+ public static final int SORT_ORDER_SIZE = MENU_OPTIONS_BASE + 1;
+ // Filter options used for displayed list of applications
+ public static final int FILTER_APPS_ALL = MENU_OPTIONS_BASE + 2;
+ public static final int FILTER_APPS_THIRD_PARTY = MENU_OPTIONS_BASE + 3;
+ public static final int FILTER_APPS_RUNNING = MENU_OPTIONS_BASE + 4;
+ // sort order
+ private int mSortOrder = SORT_ORDER_ALPHA;
+ // Filter value
+ int mFilterApps = FILTER_APPS_ALL;
+
+ // Custom Adapter used for managing items in the list
+ private AppInfoAdapter mAppInfoAdapter;
+
+ // messages posted to the handler
+ private static final int HANDLER_MESSAGE_BASE = 0;
+ private static final int COMPUTE_PKG_SIZE_START = HANDLER_MESSAGE_BASE+1;
+ private static final int COMPUTE_PKG_SIZE_DONE = HANDLER_MESSAGE_BASE+2;
+ private static final int REMOVE_PKG = HANDLER_MESSAGE_BASE+3;
+ private static final int REORDER_LIST = HANDLER_MESSAGE_BASE+4;
+ private static final int ADD_PKG_START = HANDLER_MESSAGE_BASE+5;
+ private static final int ADD_PKG_DONE = HANDLER_MESSAGE_BASE+6;
+ private static final int REFRESH_ICONS = HANDLER_MESSAGE_BASE+7;
+
+ // observer object used for computing pkg sizes
private PkgSizeObserver mObserver;
+ // local handle to PackageManager
private PackageManager mPm;
+ // Broadcast Receiver object that receives notifications for added/deleted
+ // packages
private PackageIntentReceiver mReceiver;
+ // atomic variable used to track if computing pkg sizes is in progress. should be volatile?
+
private boolean mDoneIniting = false;
- private String mKbStr;
- private String mMbStr;
- private String mBStr;
+ // default icon thats used when displaying applications initially before resource info is
+ // retrieved
+ private Drawable mDefaultAppIcon;
+
+ // temporary dialog displayed while the application info loads
+ private ProgressDialog mLoadingDlg = null;
+
+ // compute index used to track the application size computations
+ private int mComputeIndex;
+
+ // Size resource used for packages whose size computation failed for some reason
+ private CharSequence mInvalidSizeStr;
+ private CharSequence mComputingSizeStr;
+
+ // map used to store list of added and removed packages. Immutable Boolean
+ // variables indicate if a package has been added or removed. If a package is
+ // added or deleted multiple times a single entry with the latest operation will
+ // be recorded in the map.
+ private Map mAddRemoveMap;
+
+ // layout inflater object used to inflate views
+ private LayoutInflater mInflater;
+
+ // invalid size value used initially and also when size retrieval through PackageManager
+ // fails for whatever reason
+ private static final int SIZE_INVALID = -1;
+
+ // debug boolean variable to test delays from PackageManager API's
+ private boolean DEBUG_PKG_DELAY = false;
+
+ // Thread to load resources
+ ResourceLoaderThread mResourceThread;
+
+ String mCurrentPkgName;
+
+ //TODO implement a cache system
+ private Map mAppPropCache;
/*
* Handler class to handle messages for various operations
+ * Most of the operations that effect Application related data
+ * are posted as messages to the handler to avoid synchronization
+ * when accessing these structures.
+ * When the size retrieval gets kicked off for the first time, a COMPUTE_PKG_SIZE_START
+ * message is posted to the handler which invokes the getSizeInfo for the pkg at index 0
+ * When the PackageManager's asynchronous call back through
+ * PkgSizeObserver.onGetStatsCompleted gets invoked, the application resources like
+ * label, description, icon etc., is loaded in the same thread and these values are
+ * set on the observer. The observer then posts a COMPUTE_PKG_SIZE_DONE message
+ * to the handler. This information is updated on the AppInfoAdapter associated with
+ * the list view of this activity and size info retrieval is initiated for the next package as
+ * indicated by mComputeIndex
+ * When a package gets added while the activity has focus, the PkgSizeObserver posts
+ * ADD_PKG_START message to the handler. If the computation is not in progress, the size
+ * is retrieved for the newly added package through the observer object and the newly
+ * installed app info is updated on the screen. If the computation is still in progress
+ * the package is added to an internal structure and action deferred till the computation
+ * is done for all the packages.
+ * When a package gets deleted, REMOVE_PKG is posted to the handler
+ * if computation is not in progress(as indicated by
+ * mDoneIniting), the package is deleted from the displayed list of apps. If computation is
+ * still in progress the package is added to an internal structure and action deferred till
+ * the computation is done for all packages.
+ * When the sizes of all packages is computed, the newly
+ * added or removed packages are processed in order.
+ * If the user changes the order in which these applications are viewed by hitting the
+ * menu key, REORDER_LIST message is posted to the handler. this sorts the list
+ * of items based on the sort order.
*/
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
PackageStats ps;
ApplicationInfo info;
Bundle data;
- String pkgName;
- int idx;
- int size;
- boolean chained = false;
+ String pkgName = null;
+ AppInfo appInfo;
data = msg.getData();
+ if(data != null) {
+ pkgName = data.getString(ATTR_PKG_NAME);
+ }
switch (msg.what) {
case COMPUTE_PKG_SIZE_START:
- mDoneIniting = false;
- //initialize lists
- mAppList = new ArrayList();
- mSizeMap = new HashMap();
- mAppAdapterMap = new HashMap >();
- //update application list from PackageManager
- mAppList = mPm.getInstalledApplications(0);
- if(mAppList.size() == 0) {
- return;
- }
- mCurrentPkgIdx = 0;
- mCurrentPkg = mAppList.get(0);
- if(localLOGV) Log.i(TAG, "Initiating compute sizes for first time");
- //register receiver
- mReceiver = new PackageIntentReceiver();
- mReceiver.registerReceiver();
- pkgName = mCurrentPkg.packageName;
- mObserver = new PkgSizeObserver(0);
- mObserver.invokeGetSizeInfo(pkgName, true);
+ if(localLOGV) Log.i(TAG, "Message COMPUTE_PKG_SIZE_START");
+ setProgressBarIndeterminateVisibility(true);
+ mComputeIndex = 0;
+ initAppList(mFilterApps);
break;
case COMPUTE_PKG_SIZE_DONE:
- ps = mObserver.ps;
- info = mObserver.appInfo;
- chained = data.getBoolean(ATTR_CHAINED);
- if(!mObserver.succeeded) {
- if(chained) {
- removePackageFromAppList(ps.packageName);
- } else {
- //do not go to adding phase
+ if(localLOGV) Log.i(TAG, "Message COMPUTE_PKG_SIZE_DONE");
+ if(pkgName == null) {
+ Log.w(TAG, "Ignoring message");
+ break;
+ }
+ ps = data.getParcelable(ATTR_APP_PKG_STATS);
+ if(ps == null) {
+ Log.i(TAG, "Invalid package stats for package:"+pkgName);
+ } else {
+ int pkgId = mAppInfoAdapter.getIndex(pkgName);
+ if(mComputeIndex != pkgId) {
+ //spurious call from stale observer
+ Log.w(TAG, "Stale call back from PkgSizeObserver");
break;
}
- } else {
- //insert size value
- mSizeMap.put(ps.packageName, ps);
- Map entry = createMapEntry(mPm.getApplicationLabel(info),
- mPm.getApplicationIcon(info),
- info.loadDescription(mPm),
- getSizeStr(ps));
- mAppAdapterMap.put(ps.packageName, entry);
+ mAppInfoAdapter.updateAppSize(pkgName, ps);
}
- if(chained) {
- //here app list is precomputed
- idx = data.getInt(ATTR_APP_IDX);
- //increment only if succeded
- if(mObserver.succeeded) {
- idx++;
- }
- if(idx < mAppList.size()) {
- pkgName = mAppList.get(idx).packageName;
- //increment record index and invoke getSizeInfo for next record
- mObserver.invokeGetSizeInfo(pkgName, true);
- } else {
- sortAppList();
- createListFromValues();
- mDoneIniting = true;
- }
+ mComputeIndex++;
+ if (mComputeIndex < mAppInfoAdapter.getCount()) {
+ // initiate compute package size for next pkg in list
+ mObserver.invokeGetSizeInfo(mAppInfoAdapter.getApplicationInfo(
+ mComputeIndex),
+ COMPUTE_PKG_SIZE_DONE);
} else {
- //add app info object as well
- mAppList.add(info);
- sortAppList();
- size = mAppList.size();
- int i;
- for(i = 0; i < size; i++) {
- if(mAppList.get(i).packageName.equalsIgnoreCase(mCurrentPkg.packageName)) {
- if(i > mCurrentPkgIdx) {
- mCurrentPkgIdx = i;
- }
- break;
+ // check for added/removed packages
+ Set keys = mAddRemoveMap.keySet();
+ Iterator iter = keys.iterator();
+ List removeList = new ArrayList();
+ boolean added = false;
+ boolean removed = false;
+ while (iter.hasNext()) {
+ String key = iter.next();
+ if (mAddRemoveMap.get(key) == Boolean.TRUE) {
+ // add
+ try {
+ info = mPm.getApplicationInfo(key, 0);
+ mAppInfoAdapter.addApplicationInfo(info);
+ added = true;
+ } catch (NameNotFoundException e) {
+ Log.w(TAG, "Invalid added package:"+key+" Ignoring entry");
+ }
+ } else {
+ // remove
+ removeList.add(key);
+ removed = true;
}
}
- createListFromValues();
+ // remove uninstalled packages from list
+ if (removed) {
+ mAppInfoAdapter.removeFromList(removeList);
+ }
+ // handle newly installed packages
+ if (added) {
+ mObserver.invokeGetSizeInfo(mAppInfoAdapter.getApplicationInfo(
+ mComputeIndex),
+ COMPUTE_PKG_SIZE_DONE);
+ } else {
+ // end computation here
+ mDoneIniting = true;
+ mAppInfoAdapter.sortList(mSortOrder);
+ //load resources now
+ if(mResourceThread.isAlive()) {
+ mResourceThread.interrupt();
+ }
+ mResourceThread.loadAllResources(mAppInfoAdapter.getAppList());
+ }
}
break;
case REMOVE_PKG:
- if(!mDoneIniting) {
- //insert message again after some delay
- sendMessageToHandler(REMOVE_PKG, data, 10*1000);
+ if(localLOGV) Log.i(TAG, "Message REMOVE_PKG");
+ if(pkgName == null) {
+ Log.w(TAG, "Ignoring message:REMOVE_PKG for null pkgName");
break;
}
- pkgName = data.getString(ATTR_PKG_NAME);
- removePackageFromAppList(pkgName);
- if(mSizeMap.remove(pkgName) == null) {
- Log.i(TAG, "Coudnt remove from size map package:"+pkgName);
- }
- if(mAppAdapterMap.remove(pkgName) == null) {
- Log.i(TAG, "Coudnt remove from app adapter map package:"+pkgName);
- }
- if(mCurrentPkg.packageName.equalsIgnoreCase(pkgName)) {
- if(mCurrentPkgIdx == (mAppList.size()-1)) {
- mCurrentPkgIdx--;
+ if (!mDoneIniting) {
+ Boolean currB = mAddRemoveMap.get(pkgName);
+ if (currB == null || (currB.equals(Boolean.TRUE))) {
+ mAddRemoveMap.put(pkgName, Boolean.FALSE);
}
- mCurrentPkg = mAppList.get(mCurrentPkgIdx);
+ break;
}
- createListFromValues();
+ List pkgList = new ArrayList();
+ pkgList.add(pkgName);
+ mAppInfoAdapter.removeFromList(pkgList);
break;
case REORDER_LIST:
- int sortOrder = msg.arg1;
- if(sortOrder != mSortOrder) {
- mSortOrder = sortOrder;
- if(localLOGV) Log.i(TAG, "Changing sort order to "+mSortOrder);
- sortAppList();
- mCurrentPkgIdx = 0;
- mCurrentPkg = mAppList.get(mCurrentPkgIdx);
- createListFromValues();
+ if(localLOGV) Log.i(TAG, "Message REORDER_LIST");
+ int menuOption = msg.arg1;
+ if((menuOption == SORT_ORDER_ALPHA) ||
+ (menuOption == SORT_ORDER_SIZE)) {
+ // Option to sort list
+ if (menuOption != mSortOrder) {
+ mSortOrder = menuOption;
+ if (localLOGV) Log.i(TAG, "Changing sort order to "+mSortOrder);
+ mAppInfoAdapter.sortList(mSortOrder);
+ }
+ } else if(menuOption != mFilterApps) {
+ // Option to filter list
+ mFilterApps = menuOption;
+ boolean ret = mAppInfoAdapter.resetAppList(mFilterApps,
+ getInstalledApps(mFilterApps));
+ if(!ret) {
+ // Reset cache
+ mAppPropCache = null;
+ mFilterApps = FILTER_APPS_ALL;
+ mHandler.sendEmptyMessage(COMPUTE_PKG_SIZE_START);
+ sendMessageToHandler(REORDER_LIST, menuOption);
+ }
}
break;
- case ADD_PKG:
- pkgName = data.getString(ATTR_PKG_NAME);
- if(!mDoneIniting) {
- //insert message again after some delay
- sendMessageToHandler(ADD_PKG, data, 10*1000);
+ case ADD_PKG_START:
+ if(localLOGV) Log.i(TAG, "Message ADD_PKG_START");
+ if(pkgName == null) {
+ Log.w(TAG, "Ignoring message:ADD_PKG_START for null pkgName");
break;
}
- mObserver.invokeGetSizeInfo(pkgName, false);
+ if (!mDoneIniting) {
+ Boolean currB = mAddRemoveMap.get(pkgName);
+ if (currB == null || (currB.equals(Boolean.FALSE))) {
+ mAddRemoveMap.put(pkgName, Boolean.TRUE);
+ }
+ break;
+ }
+ try {
+ info = mPm.getApplicationInfo(pkgName, 0);
+ } catch (NameNotFoundException e) {
+ Log.w(TAG, "Couldnt find application info for:"+pkgName);
+ break;
+ }
+ mObserver.invokeGetSizeInfo(info, ADD_PKG_DONE);
break;
+ case ADD_PKG_DONE:
+ if(localLOGV) Log.i(TAG, "Message COMPUTE_PKG_SIZE_DONE");
+ if(pkgName == null) {
+ Log.w(TAG, "Ignoring message:ADD_PKG_START for null pkgName");
+ break;
+ }
+ ps = data.getParcelable(ATTR_APP_PKG_STATS);
+ mAppInfoAdapter.addToList(pkgName, ps);
+ break;
+ case REFRESH_ICONS:
+ Map iconMap = (Map) msg.obj;
+ if(iconMap == null) {
+ Log.w(TAG, "Error loading icons for applications");
+ } else {
+ mAppInfoAdapter.updateAppsResourceInfo(iconMap);
+ setProgressBarIndeterminateVisibility(false);
+ }
default:
break;
}
}
};
- private void removePackageFromAppList(String pkgName) {
- int size = mAppList.size();
- for(int i = 0; i < size; i++) {
- if(mAppList.get(i).packageName.equalsIgnoreCase(pkgName)) {
- mAppList.remove(i);
- break;
+ List getInstalledApps(int filterOption) {
+ List installedAppList = mPm.getInstalledApplications(
+ PackageManager.GET_UNINSTALLED_PACKAGES);
+ if (installedAppList == null) {
+ return new ArrayList ();
+ }
+ if (filterOption == FILTER_APPS_THIRD_PARTY) {
+ List appList =new ArrayList ();
+ for (ApplicationInfo appInfo : installedAppList) {
+ if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
+ appList.add(appInfo);
+ }
}
- }
- }
-
- private void clearMessages() {
- synchronized(mHandler) {
- mHandler.removeMessages(COMPUTE_PKG_SIZE_START);
- mHandler.removeMessages(COMPUTE_PKG_SIZE_DONE);
- mHandler.removeMessages(REMOVE_PKG);
- mHandler.removeMessages(REORDER_LIST);
- mHandler.removeMessages(ADD_PKG);
- }
- }
-
- private void sendMessageToHandler(int msgId, Bundle data, long delayMillis) {
- synchronized(mHandler) {
- Message msg = mHandler.obtainMessage(msgId);
- msg.setData(data);
- if(delayMillis == 0) {
- mHandler.sendMessage(msg);
- } else {
- mHandler.sendMessageDelayed(msg, delayMillis);
+ return appList;
+ } else if (filterOption == FILTER_APPS_RUNNING) {
+ List appList =new ArrayList ();
+ List procList = getRunningAppProcessesList();
+ if ((procList == null) || (procList.size() == 0)) {
+ return appList;
}
+ // Retrieve running processes from ActivityManager
+ for (ActivityManager.RunningAppProcessInfo appProcInfo : procList) {
+ if ((appProcInfo != null) && (appProcInfo.pkgList != null)){
+ int size = appProcInfo.pkgList.length;
+ for (int i = 0; i < size; i++) {
+ ApplicationInfo appInfo = null;
+ try {
+ appInfo = mPm.getApplicationInfo(appProcInfo.pkgList[i],
+ PackageManager.GET_UNINSTALLED_PACKAGES);
+ } catch (NameNotFoundException e) {
+ Log.w(TAG, "Error retrieving ApplicationInfo for pkg:"+appProcInfo.pkgList[i]);
+ continue;
+ }
+ if(appInfo != null) {
+ appList.add(appInfo);
+ }
+ }
+ }
+ }
+ return appList;
+ } else {
+ return installedAppList;
}
}
- private void sendMessageToHandler(int msgId, int arg1) {
- synchronized(mHandler) {
- Message msg = mHandler.obtainMessage(msgId);
- msg.arg1 = arg1;
+ private List getRunningAppProcessesList() {
+ ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
+ return am.getRunningAppProcesses();
+ }
+
+ // some initialization code used when kicking off the size computation
+ private void initAppList(int filterOption) {
+ mDoneIniting = false;
+ // Initialize lists
+ List appList = getInstalledApps(filterOption);
+ mAddRemoveMap = new TreeMap();
+ mAppInfoAdapter = new AppInfoAdapter(this, appList);
+ dismissLoadingMsg();
+ // get list and set listeners and adapter
+ ListView lv= (ListView) findViewById(android.R.id.list);
+ lv.setOnItemClickListener(this);
+ lv.setSaveEnabled(true);
+ lv.setItemsCanFocus(true);
+ lv.setOnItemClickListener(this);
+ lv.setAdapter(mAppInfoAdapter);
+ // register receiver
+ mReceiver = new PackageIntentReceiver();
+ mReceiver.registerReceiver();
+ // initiate compute pkg sizes
+ if (localLOGV) Log.i(TAG, "Initiating compute sizes for first time");
+ mObserver = new PkgSizeObserver();
+ if(appList.size() > 0) {
+ mObserver.invokeGetSizeInfo(appList.get(0), COMPUTE_PKG_SIZE_DONE);
+ } else {
+ mDoneIniting = true;
+ }
+ }
+
+ // internal structure used to track added and deleted packages when
+ // the activity has focus
+ class AddRemoveInfo {
+ String pkgName;
+ boolean add;
+ public AddRemoveInfo(String pPkgName, boolean pAdd) {
+ pkgName = pPkgName;
+ add = pAdd;
+ }
+ }
+
+ class ResourceLoaderThread extends Thread {
+ List mAppList;
+
+ void loadAllResources(List appList) {
+ if(appList == null || appList.size() <= 0) {
+ Log.w(TAG, "Empty or null application list");
+ return;
+ }
+ mAppList = appList;
+ start();
+ }
+
+ public void run() {
+ Map iconMap = new HashMap();
+ for (ApplicationInfo appInfo : mAppList) {
+ CharSequence appName = appInfo.loadLabel(mPm);
+ Drawable appIcon = appInfo.loadIcon(mPm);
+ iconMap.put(appInfo.packageName,
+ new AppInfo(appInfo.packageName, appName, appIcon));
+ }
+ Message msg = mHandler.obtainMessage(REFRESH_ICONS);
+ msg.obj = iconMap;
mHandler.sendMessage(msg);
}
}
- private void sendMessageToHandler(int msgId) {
- synchronized(mHandler) {
- mHandler.sendEmptyMessage(msgId);
+ /* Internal class representing an application or packages displayable attributes
+ *
+ */
+ class AppInfo {
+ public String pkgName;
+ int index;
+ public CharSequence appName;
+ public Drawable appIcon;
+ public CharSequence appSize;
+ public PackageStats appStats;
+
+ public void refreshIcon(AppInfo pInfo) {
+ appName = pInfo.appName;
+ appIcon = pInfo.appIcon;
+ }
+
+ public AppInfo(String pName, CharSequence aName, Drawable aIcon) {
+ index = -1;
+ pkgName = pName;
+ appName = aName;
+ appIcon = aIcon;
+ appStats = null;
+ appSize = mComputingSizeStr;
+ }
+
+ public AppInfo(String pName, int pIndex, CharSequence aName, Drawable aIcon,
+ PackageStats ps) {
+ index = pIndex;
+ pkgName = pName;
+ appName = aName;
+ appIcon = aIcon;
+ if(ps == null) {
+ appSize = mComputingSizeStr;
+ } else {
+ appStats = ps;
+ appSize = getSizeStr();
+ }
+ }
+ public void setSize(PackageStats ps) {
+ appStats = ps;
+ if (ps != null) {
+ appSize = getSizeStr();
+ }
+ }
+ public long getTotalSize() {
+ PackageStats ps = appStats;
+ if (ps != null) {
+ return ps.cacheSize+ps.codeSize+ps.dataSize;
+ }
+ return SIZE_INVALID;
+ }
+
+ private String getSizeStr() {
+ PackageStats ps = appStats;
+ String retStr = "";
+ // insert total size information into map to display in view
+ // at this point its guaranteed that ps is not null. but checking anyway
+ if (ps != null) {
+ long size = getTotalSize();
+ if (size == SIZE_INVALID) {
+ return mInvalidSizeStr.toString();
+ }
+ return Formatter.formatFileSize(ManageApplications.this, size);
+ }
+ return retStr;
}
}
- class PkgSizeObserver extends IPackageStatsObserver.Stub {
- public PackageStats ps;
- public ApplicationInfo appInfo;
- public Drawable appIcon;
- public CharSequence appName;
- public CharSequence appDesc = "";
- private int mIdx = 0;
- private boolean mChained = false;
- public boolean succeeded;
- PkgSizeObserver(int i) {
- mIdx = i;
- }
+ // View Holder used when displaying views
+ static class AppViewHolder {
+ TextView appName;
+ ImageView appIcon;
+ TextView appSize;
+ }
+
+ /* Custom adapter implementation for the ListView
+ * This adapter maintains a map for each displayed application and its properties
+ * An index value on each AppInfo object indicates the correct position or index
+ * in the list. If the list gets updated dynamically when the user is viewing the list of
+ * applications, we need to return the correct index of position. This is done by mapping
+ * the getId methods via the package name into the internal maps and indices.
+ * The order of applications in the list is mirrored in mAppLocalList
+ */
+ class AppInfoAdapter extends BaseAdapter {
+ private Map mAppPropMap;
+ private List mAppLocalList;
+ ApplicationInfo.DisplayNameComparator mAlphaComparator;
+ AppInfoComparator mSizeComparator;
- private void getAppDetails() {
- try {
- appInfo = mPm.getApplicationInfo(ps.packageName, 0);
- } catch (NameNotFoundException e) {
- return;
+ private AppInfo getFromCache(String packageName) {
+ if(mAppPropCache == null) {
+ return null;
}
- appName = appInfo.loadLabel(mPm);
- appIcon = appInfo.loadIcon(mPm);
+ return mAppPropCache.get(packageName);
}
- public void onGetStatsCompleted(PackageStats pStats, boolean pSucceeded) {
- Bundle data = new Bundle();
- ps = pStats;
- succeeded = pSucceeded;
- if(mChained) {
- data.putInt(ATTR_APP_IDX, mIdx);
- if(succeeded) {
- mIdx++;
+ public AppInfoAdapter(Context c, List appList) {
+ mAppLocalList = appList;
+ boolean useCache = false;
+ int sortOrder = SORT_ORDER_ALPHA;
+ int imax = mAppLocalList.size();
+ if(mAppPropCache != null) {
+ useCache = true;
+ // Activity has been resumed. can use the cache to populate values initially
+ mAppPropMap = mAppPropCache;
+ sortOrder = mSortOrder;
+ }
+ sortAppList(sortOrder);
+ // Recreate property map
+ mAppPropMap = new TreeMap();
+ for (int i = 0; i < imax; i++) {
+ ApplicationInfo info = mAppLocalList.get(i);
+ AppInfo aInfo = getFromCache(info.packageName);
+ if(aInfo == null){
+ aInfo = new AppInfo(info.packageName, i,
+ info.packageName, mDefaultAppIcon, null);
+ } else {
+ aInfo.index = i;
+ }
+ mAppPropMap.put(info.packageName, aInfo);
+ }
+ }
+
+ public int getCount() {
+ return mAppLocalList.size();
+ }
+
+ public Object getItem(int position) {
+ return mAppLocalList.get(position);
+ }
+
+ /*
+ * This method returns the index of the package position in the application list
+ */
+ public int getIndex(String pkgName) {
+ if(pkgName == null) {
+ Log.w(TAG, "Getting index of null package in List Adapter");
+ }
+ int imax = mAppLocalList.size();
+ ApplicationInfo appInfo;
+ for(int i = 0; i < imax; i++) {
+ appInfo = mAppLocalList.get(i);
+ if(appInfo.packageName.equalsIgnoreCase(pkgName)) {
+ return i;
}
}
- data.putBoolean(ATTR_CHAINED, mChained);
- getAppDetails();
- if(localLOGV) Log.i(TAG, "onGetStatsCompleted::"+appInfo.packageName+", ("+ps.cacheSize+","+
- ps.codeSize+", "+ps.dataSize);
- sendMessageToHandler(COMPUTE_PKG_SIZE_DONE, data, 0);
+ return -1;
}
- public void invokeGetSizeInfo(String packageName, boolean chained) {
- mChained = chained;
- mPm.getPackageSizeInfo(packageName, this);
+ public ApplicationInfo getApplicationInfo(int position) {
+ int imax = mAppLocalList.size();
+ if( (position < 0) || (position >= imax)) {
+ Log.w(TAG, "Position out of bounds in List Adapter");
+ return null;
+ }
+ return mAppLocalList.get(position);
+ }
+
+ public void addApplicationInfo(ApplicationInfo info) {
+ if(info == null) {
+ Log.w(TAG, "Ignoring null add in List Adapter");
+ return;
+ }
+ mAppLocalList.add(info);
+ }
+
+ public long getItemId(int position) {
+ int imax = mAppLocalList.size();
+ if( (position < 0) || (position >= imax)) {
+ Log.w(TAG, "Position out of bounds in List Adapter");
+ return -1;
+ }
+ return mAppPropMap.get(mAppLocalList.get(position).packageName).index;
+ }
+
+ public List getAppList() {
+ return mAppLocalList;
+ }
+
+ public View getView(int position, View convertView, ViewGroup parent) {
+ // A ViewHolder keeps references to children views to avoid unneccessary calls
+ // to findViewById() on each row.
+ AppViewHolder holder;
+
+ // When convertView is not null, we can reuse it directly, there is no need
+ // to reinflate it. We only inflate a new View when the convertView supplied
+ // by ListView is null.
+ if (convertView == null) {
+ convertView = mInflater.inflate(R.layout.manage_applications_item, null);
+
+ // Creates a ViewHolder and store references to the two children views
+ // we want to bind data to.
+ holder = new AppViewHolder();
+ holder.appName = (TextView) convertView.findViewById(R.id.app_name);
+ holder.appIcon = (ImageView) convertView.findViewById(R.id.app_icon);
+ holder.appSize = (TextView) convertView.findViewById(R.id.app_size);
+ convertView.setTag(holder);
+ } else {
+ // Get the ViewHolder back to get fast access to the TextView
+ // and the ImageView.
+ holder = (AppViewHolder) convertView.getTag();
+ }
+
+ // Bind the data efficiently with the holder
+ ApplicationInfo appInfo = mAppLocalList.get(position);
+ AppInfo mInfo = mAppPropMap.get(appInfo.packageName);
+ if(mInfo != null) {
+ if(mInfo.appName != null) {
+ holder.appName.setText(mInfo.appName);
+ }
+ if(mInfo.appIcon != null) {
+ holder.appIcon.setImageDrawable(mInfo.appIcon);
+ }
+ holder.appSize.setText(mInfo.appSize);
+ } else {
+ Log.w(TAG, "No info for package:"+appInfo.packageName+" in property map");
+ }
+ return convertView;
+ }
+
+ private void adjustIndex() {
+ int imax = mAppLocalList.size();
+ ApplicationInfo info;
+ for (int i = 0; i < imax; i++) {
+ info = mAppLocalList.get(i);
+ mAppPropMap.get(info.packageName).index = i;
+ }
+ }
+
+ public void sortAppList(int sortOrder) {
+ Collections.sort(mAppLocalList, getAppComparator(sortOrder));
+ }
+
+ public void sortList(int sortOrder) {
+ sortAppList(sortOrder);
+ adjustIndex();
+ notifyDataSetChanged();
+ }
+
+ public boolean resetAppList(int filterOption, List appList) {
+ // Create application list based on the filter value
+ mAppLocalList = appList;
+ // Check for all properties in map before sorting. Populate values from cache
+ for(ApplicationInfo applicationInfo : mAppLocalList) {
+ AppInfo appInfo = mAppPropMap.get(applicationInfo.packageName);
+ if(appInfo == null) {
+ AppInfo rInfo = getFromCache(applicationInfo.packageName);
+ if(rInfo == null) {
+ // Need to load resources again. Inconsistency somewhere
+ return false;
+ }
+ mAppPropMap.put(applicationInfo.packageName, rInfo);
+ }
+ }
+ sortList(mSortOrder);
+ return true;
+ }
+
+ private Comparator getAppComparator(int sortOrder) {
+ if (sortOrder == SORT_ORDER_ALPHA) {
+ // Lazy initialization
+ if (mAlphaComparator == null) {
+ mAlphaComparator = new ApplicationInfo.DisplayNameComparator(mPm);
+ }
+ return mAlphaComparator;
+ }
+ // Lazy initialization
+ if(mSizeComparator == null) {
+ mSizeComparator = new AppInfoComparator(mAppPropMap);
+ }
+ return mSizeComparator;
+ }
+
+ public void updateAppsResourceInfo(Map iconMap) {
+ if(iconMap == null) {
+ Log.w(TAG, "Null iconMap when refreshing icon in List Adapter");
+ return;
+ }
+ boolean changed = false;
+ for (ApplicationInfo info : mAppLocalList) {
+ AppInfo pInfo = iconMap.get(info.packageName);
+ if(pInfo != null) {
+ AppInfo aInfo = mAppPropMap.get(info.packageName);
+ aInfo.refreshIcon(pInfo);
+ changed = true;
+ }
+ }
+ if(changed) {
+ notifyDataSetChanged();
+ }
+ }
+
+ public void addToList(String pkgName, PackageStats ps) {
+ if(pkgName == null) {
+ Log.w(TAG, "Adding null pkg to List Adapter");
+ return;
+ }
+ ApplicationInfo info;
+ try {
+ info = mPm.getApplicationInfo(pkgName, 0);
+ } catch (NameNotFoundException e) {
+ Log.w(TAG, "Ignoring non-existent package:"+pkgName);
+ return;
+ }
+ if(info == null) {
+ // Nothing to do log error message and return
+ Log.i(TAG, "Null ApplicationInfo for package:"+pkgName);
+ return;
+ }
+ // Binary search returns a negative index (ie --index) of the position where
+ // this might be inserted.
+ int newIdx = Collections.binarySearch(mAppLocalList, info,
+ getAppComparator(mSortOrder));
+ if(newIdx >= 0) {
+ Log.i(TAG, "Strange. Package:"+pkgName+" is not new");
+ return;
+ }
+ // New entry
+ newIdx = -newIdx-1;
+ mAppLocalList.add(newIdx, info);
+ mAppPropMap.put(info.packageName, new AppInfo(pkgName, newIdx,
+ info.loadLabel(mPm), info.loadIcon(mPm), ps));
+ adjustIndex();
+ notifyDataSetChanged();
+ }
+
+ public void removeFromList(List pkgNames) {
+ if(pkgNames == null) {
+ Log.w(TAG, "Removing null pkg list from List Adapter");
+ return;
+ }
+ int imax = mAppLocalList.size();
+ boolean found = false;
+ ApplicationInfo info;
+ int i, k;
+ String pkgName;
+ int kmax = pkgNames.size();
+ if(kmax <= 0) {
+ Log.w(TAG, "Removing empty pkg list from List Adapter");
+ return;
+ }
+ int idxArr[] = new int[kmax];
+ for (k = 0; k < kmax; k++) {
+ idxArr[k] = -1;
+ }
+ for (i = 0; i < imax; i++) {
+ info = mAppLocalList.get(i);
+ for (k = 0; k < kmax; k++) {
+ pkgName = pkgNames.get(k);
+ if (info.packageName.equalsIgnoreCase(pkgName)) {
+ idxArr[k] = i;
+ found = true;
+ break;
+ }
+ }
+ }
+ // Sort idxArr
+ Arrays.sort(idxArr);
+ // remove the packages based on decending indices
+ for (k = kmax-1; k >= 0; k--) {
+ // Check if package has been found in the list of existing apps first
+ if(idxArr[k] == -1) {
+ break;
+ }
+ info = mAppLocalList.get(idxArr[k]);
+ mAppLocalList.remove(idxArr[k]);
+ mAppPropMap.remove(info.packageName);
+ if (localLOGV) Log.i(TAG, "Removed pkg:"+info.packageName+ " list");
+ }
+ if (found) {
+ adjustIndex();
+ notifyDataSetChanged();
+ }
+ }
+
+ public void updateAppSize(String pkgName, PackageStats ps) {
+ if(pkgName == null) {
+ return;
+ }
+ AppInfo entry = mAppPropMap.get(pkgName);
+ if (entry == null) {
+ Log.w(TAG, "Entry for package:"+pkgName+"doesnt exist in map");
+ return;
+ }
+ // Copy the index into the newly updated entry
+ entry.setSize(ps);
+ notifyDataSetChanged();
+ }
+
+ public PackageStats getAppStats(String pkgName) {
+ if(pkgName == null) {
+ return null;
+ }
+ AppInfo entry = mAppPropMap.get(pkgName);
+ if (entry == null) {
+ return null;
+ }
+ return entry.appStats;
+ }
+ }
+
+ /*
+ * Utility method to clear messages to Handler
+ * We need'nt synchronize on the Handler since posting messages is guaranteed
+ * to be thread safe. Even if the other thread that retrieves package sizes
+ * posts a message, we do a cursory check of validity on mAppInfoAdapter's applist
+ */
+ private void clearMessagesInHandler() {
+ mHandler.removeMessages(COMPUTE_PKG_SIZE_START);
+ mHandler.removeMessages(COMPUTE_PKG_SIZE_DONE);
+ mHandler.removeMessages(REMOVE_PKG);
+ mHandler.removeMessages(REORDER_LIST);
+ mHandler.removeMessages(ADD_PKG_START);
+ mHandler.removeMessages(ADD_PKG_DONE);
+ }
+
+ private void sendMessageToHandler(int msgId, int arg1) {
+ Message msg = mHandler.obtainMessage(msgId);
+ msg.arg1 = arg1;
+ mHandler.sendMessage(msg);
+ }
+
+ private void sendMessageToHandler(int msgId, Bundle data) {
+ Message msg = mHandler.obtainMessage(msgId);
+ msg.setData(data);
+ mHandler.sendMessage(msg);
+ }
+
+ private void sendMessageToHandler(int msgId) {
+ mHandler.sendEmptyMessage(msgId);
+ }
+
+ /*
+ * Stats Observer class used to compute package sizes and retrieve size information
+ * PkgSizeOberver is the call back thats used when invoking getPackageSizeInfo on
+ * PackageManager. The values in call back onGetStatsCompleted are validated
+ * and the specified message is passed to mHandler. The package name
+ * and the AppInfo object corresponding to the package name are set on the message
+ */
+ class PkgSizeObserver extends IPackageStatsObserver.Stub {
+ private ApplicationInfo mAppInfo;
+ private int mMsgId;
+ public void onGetStatsCompleted(PackageStats pStats, boolean pSucceeded) {
+ if(DEBUG_PKG_DELAY) {
+ try {
+ Thread.sleep(10*1000);
+ } catch (InterruptedException e) {
+ }
+ }
+ AppInfo appInfo = null;
+ Bundle data = new Bundle();
+ data.putString(ATTR_PKG_NAME, mAppInfo.packageName);
+ if(pSucceeded && pStats != null) {
+ if (localLOGV) Log.i(TAG, "onGetStatsCompleted::"+pStats.packageName+", ("+
+ pStats.cacheSize+","+
+ pStats.codeSize+", "+pStats.dataSize);
+ data.putParcelable(ATTR_APP_PKG_STATS, pStats);
+ } else {
+ Log.w(TAG, "Invalid package stats from PackageManager");
+ }
+ //post message to Handler
+ Message msg = mHandler.obtainMessage(mMsgId, data);
+ msg.setData(data);
+ mHandler.sendMessage(msg);
+ }
+
+ public void invokeGetSizeInfo(ApplicationInfo pAppInfo, int msgId) {
+ if(pAppInfo == null || pAppInfo.packageName == null) {
+ return;
+ }
+ if(localLOGV) Log.i(TAG, "Invoking getPackageSizeInfo for package:"+
+ pAppInfo.packageName);
+ mMsgId = msgId;
+ mAppInfo = pAppInfo;
+ mPm.getPackageSizeInfo(pAppInfo.packageName, this);
}
}
@@ -360,239 +998,147 @@ public class ManageApplications extends Activity implements SimpleAdapter.ViewBi
String actionStr = intent.getAction();
Uri data = intent.getData();
String pkgName = data.getEncodedSchemeSpecificPart();
- if(localLOGV) Log.i(TAG, "action:"+actionStr+", for package:"+pkgName);
+ if (localLOGV) Log.i(TAG, "action:"+actionStr+", for package:"+pkgName);
updatePackageList(actionStr, pkgName);
}
}
private void updatePackageList(String actionStr, String pkgName) {
- //technically we dont have to invoke handler since onReceive is invoked on
- //the main thread but doing it here for better clarity
- if(Intent.ACTION_PACKAGE_ADDED.equalsIgnoreCase(actionStr)) {
+ // technically we dont have to invoke handler since onReceive is invoked on
+ // the main thread but doing it here for better clarity
+ if (Intent.ACTION_PACKAGE_ADDED.equalsIgnoreCase(actionStr)) {
Bundle data = new Bundle();
data.putString(ATTR_PKG_NAME, pkgName);
- sendMessageToHandler(ADD_PKG, data, 0);
- } else if(Intent.ACTION_PACKAGE_REMOVED.equalsIgnoreCase(actionStr)) {
+ sendMessageToHandler(ADD_PKG_START, data);
+ } else if (Intent.ACTION_PACKAGE_REMOVED.equalsIgnoreCase(actionStr)) {
Bundle data = new Bundle();
data.putString(ATTR_PKG_NAME, pkgName);
- sendMessageToHandler(REMOVE_PKG, data, 0);
- } else if(Intent.ACTION_PACKAGE_CHANGED.equalsIgnoreCase(actionStr)) {
- //force adapter to draw the list again. TODO derive from SimpleAdapter
- //to avoid this
-
- }
- }
-
- /*
- * Utility method to create an array of map objects from a map of map objects
- * for displaying list items to be used in SimpleAdapter.
- */
- private void createListFromValues() {
- findViewById(R.id.center_text).setVisibility(View.GONE);
- populateAdapterList();
- mAppAdapter.setViewBinder(this);
- ListView lv= (ListView) findViewById(android.R.id.list);
- lv.setOnItemClickListener(this);
- lv.setAdapter(mAppAdapter);
- if(mCurrentPkgIdx != -1) {
- lv.setSelection(mCurrentPkgIdx);
+ sendMessageToHandler(REMOVE_PKG, data);
}
}
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- String action = getIntent().getAction();
- if(action.equals(Intent.ACTION_MANAGE_PACKAGE_STORAGE)) {
+ Intent lIntent = getIntent();
+ String action = lIntent.getAction();
+ if (action.equals(Intent.ACTION_MANAGE_PACKAGE_STORAGE)) {
mSortOrder = SORT_ORDER_SIZE;
}
mPm = getPackageManager();
- //load strings from resources
- mBStr = getString(R.string.b_text);
- mKbStr = getString(R.string.kb_text);
- mMbStr = getString(R.string.mb_text);
+ // initialize some window features
+ requestWindowFeature(Window.FEATURE_RIGHT_ICON);
+ requestWindowFeature(Window.FEATURE_PROGRESS);
+ requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
+ // init mLoadingDlg
+ mLoadingDlg = new ProgressDialog(this);
+ mLoadingDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
+ mLoadingDlg.setMessage(getText(R.string.loading));
+ mLoadingDlg.setIndeterminate(true);
+ mLoadingDlg.setOnCancelListener(this);
+ mDefaultAppIcon =Resources.getSystem().getDrawable(
+ com.android.internal.R.drawable.sym_def_app_icon);
+ mInvalidSizeStr = getText(R.string.invalid_size_value);
+ mComputingSizeStr = getText(R.string.computing_size);
+ // initialize the inflater
+ mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ }
+
+ private void showLoadingMsg() {
+ if (mLoadingDlg != null) {
+ if(localLOGV) Log.i(TAG, "Displaying Loading message");
+ mLoadingDlg.show();
+ }
+ }
+
+ private void dismissLoadingMsg() {
+ if ((mLoadingDlg != null) && (mLoadingDlg.isShowing())) {
+ if(localLOGV) Log.i(TAG, "Dismissing Loading message");
+ mLoadingDlg.dismiss();
+ }
}
@Override
public void onStart() {
super.onStart();
setContentView(R.layout.compute_sizes);
- //clear all messages related to application list
- clearMessages();
+ showLoadingMsg();
+ // Create a thread to load resources
+ mResourceThread = new ResourceLoaderThread();
sendMessageToHandler(COMPUTE_PKG_SIZE_START);
}
@Override
public void onStop() {
super.onStop();
- //register receiver here
+ // clear all messages related to application list
+ clearMessagesInHandler();
+ // register receiver here
unregisterReceiver(mReceiver);
+ mAppPropCache = mAppInfoAdapter.mAppPropMap;
}
+ /*
+ * comparator class used to sort AppInfo objects based on size
+ */
public static class AppInfoComparator implements Comparator {
- public AppInfoComparator(HashMap pSizeMap) {
- mSizeMap= pSizeMap;
+ public AppInfoComparator(Map pAppPropMap) {
+ mAppPropMap= pAppPropMap;
}
public final int compare(ApplicationInfo a, ApplicationInfo b) {
- PackageStats aps, bps;
- aps = mSizeMap.get(a.packageName);
- bps = mSizeMap.get(b.packageName);
- if (aps == null && bps == null) {
- return 0;
- } else if (aps == null) {
- return 1;
- } else if (bps == null) {
- return -1;
- }
- long atotal = aps.dataSize+aps.codeSize+aps.cacheSize;
- long btotal = bps.dataSize+bps.codeSize+bps.cacheSize;
- long ret = atotal-btotal;
- //negate result to sort in descending order
- if(ret < 0) {
+ AppInfo ainfo = mAppPropMap.get(a.packageName);
+ AppInfo binfo = mAppPropMap.get(b.packageName);
+ long atotal = ainfo.getTotalSize();
+ long btotal = binfo.getTotalSize();
+ long ret = atotal - btotal;
+ // negate result to sort in descending order
+ if (ret < 0) {
return 1;
}
- if(ret == 0) {
+ if (ret == 0) {
return 0;
}
return -1;
}
- private HashMap mSizeMap;
+ private Map mAppPropMap;
}
-
- /*
- * Have to extract elements form map and populate a list ot be used by
- * SimpleAdapter when displaying list elements. The sort order has to follow
- * the order of elements in mAppList.
- */
- private List