Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings;
     18 
     19 import android.content.Intent;
     20 import android.os.Bundle;
     21 import android.provider.Settings;
     22 import android.support.v7.preference.CheckBoxPreference;
     23 import android.support.v7.preference.ListPreference;
     24 import android.support.v7.preference.Preference;
     25 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
     26 
     27 import com.android.internal.logging.MetricsProto.MetricsEvent;
     28 
     29 public class ApplicationSettings extends SettingsPreferenceFragment {
     30 
     31     private static final String KEY_TOGGLE_ADVANCED_SETTINGS = "toggle_advanced_settings";
     32     private static final String KEY_APP_INSTALL_LOCATION = "app_install_location";
     33 
     34     // App installation location. Default is ask the user.
     35     private static final int APP_INSTALL_AUTO = 0;
     36     private static final int APP_INSTALL_DEVICE = 1;
     37     private static final int APP_INSTALL_SDCARD = 2;
     38 
     39     private static final String APP_INSTALL_DEVICE_ID = "device";
     40     private static final String APP_INSTALL_SDCARD_ID = "sdcard";
     41     private static final String APP_INSTALL_AUTO_ID = "auto";
     42 
     43     private CheckBoxPreference mToggleAdvancedSettings;
     44     private ListPreference mInstallLocation;
     45 
     46     @Override
     47     protected int getMetricsCategory() {
     48         return MetricsEvent.APPLICATION;
     49     }
     50 
     51     @Override
     52     public void onCreate(Bundle icicle) {
     53         super.onCreate(icicle);
     54 
     55         addPreferencesFromResource(R.xml.application_settings);
     56 
     57         mToggleAdvancedSettings = (CheckBoxPreference)findPreference(
     58                 KEY_TOGGLE_ADVANCED_SETTINGS);
     59         mToggleAdvancedSettings.setChecked(isAdvancedSettingsEnabled());
     60         getPreferenceScreen().removePreference(mToggleAdvancedSettings);
     61 
     62         // not ready for prime time yet
     63         if (false) {
     64             getPreferenceScreen().removePreference(mInstallLocation);
     65         }
     66 
     67         mInstallLocation = (ListPreference) findPreference(KEY_APP_INSTALL_LOCATION);
     68         // Is app default install location set?
     69         boolean userSetInstLocation = (Settings.Global.getInt(getContentResolver(),
     70                 Settings.Global.SET_INSTALL_LOCATION, 0) != 0);
     71         if (!userSetInstLocation) {
     72             getPreferenceScreen().removePreference(mInstallLocation);
     73         } else {
     74             mInstallLocation.setValue(getAppInstallLocation());
     75             mInstallLocation.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
     76                 public boolean onPreferenceChange(Preference preference, Object newValue) {
     77                     String value = (String) newValue;
     78                     handleUpdateAppInstallLocation(value);
     79                     return false;
     80                 }
     81             });
     82         }
     83     }
     84 
     85     protected void handleUpdateAppInstallLocation(final String value) {
     86         if(APP_INSTALL_DEVICE_ID.equals(value)) {
     87             Settings.Global.putInt(getContentResolver(),
     88                     Settings.Global.DEFAULT_INSTALL_LOCATION, APP_INSTALL_DEVICE);
     89         } else if (APP_INSTALL_SDCARD_ID.equals(value)) {
     90             Settings.Global.putInt(getContentResolver(),
     91                     Settings.Global.DEFAULT_INSTALL_LOCATION, APP_INSTALL_SDCARD);
     92         } else if (APP_INSTALL_AUTO_ID.equals(value)) {
     93             Settings.Global.putInt(getContentResolver(),
     94                     Settings.Global.DEFAULT_INSTALL_LOCATION, APP_INSTALL_AUTO);
     95         } else {
     96             // Should not happen, default to prompt...
     97             Settings.Global.putInt(getContentResolver(),
     98                     Settings.Global.DEFAULT_INSTALL_LOCATION, APP_INSTALL_AUTO);
     99         }
    100         mInstallLocation.setValue(value);
    101     }
    102 
    103     @Override
    104     public boolean onPreferenceTreeClick(Preference preference) {
    105         if (preference == mToggleAdvancedSettings) {
    106             boolean value = mToggleAdvancedSettings.isChecked();
    107             setAdvancedSettingsEnabled(value);
    108         }
    109 
    110         return super.onPreferenceTreeClick(preference);
    111     }
    112 
    113     private boolean isAdvancedSettingsEnabled() {
    114         return Settings.System.getInt(getContentResolver(),
    115                                       Settings.System.ADVANCED_SETTINGS,
    116                                       Settings.System.ADVANCED_SETTINGS_DEFAULT) > 0;
    117     }
    118 
    119     private void setAdvancedSettingsEnabled(boolean enabled) {
    120         int value = enabled ? 1 : 0;
    121         // Change the system setting
    122         Settings.Secure.putInt(getContentResolver(), Settings.System.ADVANCED_SETTINGS, value);
    123         // TODO: the settings thing should broadcast this for thread safety purposes.
    124         Intent intent = new Intent(Intent.ACTION_ADVANCED_SETTINGS_CHANGED);
    125         intent.putExtra("state", value);
    126         getActivity().sendBroadcast(intent);
    127     }
    128 
    129     private String getAppInstallLocation() {
    130         int selectedLocation = Settings.Global.getInt(getContentResolver(),
    131                 Settings.Global.DEFAULT_INSTALL_LOCATION, APP_INSTALL_AUTO);
    132         if (selectedLocation == APP_INSTALL_DEVICE) {
    133             return APP_INSTALL_DEVICE_ID;
    134         } else if (selectedLocation == APP_INSTALL_SDCARD) {
    135             return APP_INSTALL_SDCARD_ID;
    136         } else  if (selectedLocation == APP_INSTALL_AUTO) {
    137             return APP_INSTALL_AUTO_ID;
    138         } else {
    139             // Default value, should not happen.
    140             return APP_INSTALL_AUTO_ID;
    141         }
    142     }
    143 }
    144