Home | History | Annotate | Download | only in development
      1 /*
      2  * Copyright (C) 2017 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.development;
     18 
     19 import android.content.Context;
     20 import android.provider.Settings;
     21 import android.support.annotation.VisibleForTesting;
     22 import android.support.v14.preference.SwitchPreference;
     23 import android.support.v7.preference.Preference;
     24 
     25 import com.android.settings.core.PreferenceControllerMixin;
     26 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
     27 
     28 public class DisableAutomaticUpdatesPreferenceController extends
     29         DeveloperOptionsPreferenceController implements
     30         Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
     31 
     32     private static final String OTA_DISABLE_AUTOMATIC_UPDATE_KEY = "ota_disable_automatic_update";
     33 
     34     // We use the "disabled status" in code, but show the opposite text
     35     // "Automatic system updates" on screen. So a value 0 indicates the
     36     // automatic update is enabled.
     37     @VisibleForTesting
     38     final static int DISABLE_UPDATES_SETTING = 1;
     39     @VisibleForTesting
     40     final static int ENABLE_UPDATES_SETTING = 0;
     41 
     42     public DisableAutomaticUpdatesPreferenceController(Context context) {
     43         super(context);
     44     }
     45 
     46     @Override
     47     public String getPreferenceKey() {
     48         return OTA_DISABLE_AUTOMATIC_UPDATE_KEY;
     49     }
     50 
     51     @Override
     52     public boolean onPreferenceChange(Preference preference, Object newValue) {
     53         final boolean updatesEnabled = (Boolean) newValue;
     54         Settings.Global.putInt(mContext.getContentResolver(),
     55                 Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE,
     56                 updatesEnabled ? ENABLE_UPDATES_SETTING : DISABLE_UPDATES_SETTING);
     57         return true;
     58     }
     59 
     60     @Override
     61     public void updateState(Preference preference) {
     62         final int updatesEnabled = Settings.Global.getInt(mContext.getContentResolver(),
     63                 Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, 0 /* default */);
     64 
     65         ((SwitchPreference) mPreference).setChecked(updatesEnabled != DISABLE_UPDATES_SETTING);
     66     }
     67 
     68     @Override
     69     protected void onDeveloperOptionsSwitchDisabled() {
     70         super.onDeveloperOptionsSwitchDisabled();
     71         Settings.Global.putInt(mContext.getContentResolver(),
     72                 Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, DISABLE_UPDATES_SETTING);
     73         ((SwitchPreference) mPreference).setChecked(false);
     74     }
     75 }
     76