1 /* 2 * Copyright (C) 2016 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.gestures; 18 19 import android.annotation.UserIdInt; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.SharedPreferences; 23 import android.provider.Settings; 24 import android.support.v7.preference.Preference; 25 26 import com.android.internal.hardware.AmbientDisplayConfiguration; 27 import com.android.settings.R; 28 import com.android.settings.search.DatabaseIndexingUtils; 29 import com.android.settings.search.InlineSwitchPayload; 30 import com.android.settings.search.ResultPayload; 31 import com.android.settingslib.core.lifecycle.Lifecycle; 32 33 import static android.provider.Settings.Secure.DOZE_PULSE_ON_PICK_UP; 34 35 public class PickupGesturePreferenceController extends GesturePreferenceController { 36 37 private final int ON = 1; 38 private final int OFF = 0; 39 40 private static final String PREF_KEY_VIDEO = "gesture_pick_up_video"; 41 private final String mPickUpPrefKey; 42 43 private final String SECURE_KEY = DOZE_PULSE_ON_PICK_UP; 44 45 private final AmbientDisplayConfiguration mAmbientConfig; 46 @UserIdInt 47 private final int mUserId; 48 49 public PickupGesturePreferenceController(Context context, Lifecycle lifecycle, 50 AmbientDisplayConfiguration config, @UserIdInt int userId, String key) { 51 super(context, lifecycle); 52 mAmbientConfig = config; 53 mUserId = userId; 54 mPickUpPrefKey = key; 55 } 56 57 public static boolean isSuggestionComplete(Context context, SharedPreferences prefs) { 58 AmbientDisplayConfiguration ambientConfig = new AmbientDisplayConfiguration(context); 59 return !ambientConfig.pulseOnPickupAvailable() 60 || prefs.getBoolean(PickupGestureSettings.PREF_KEY_SUGGESTION_COMPLETE, false); 61 } 62 63 @Override 64 public boolean isAvailable() { 65 return mAmbientConfig.pulseOnPickupAvailable(); 66 } 67 68 @Override 69 protected String getVideoPrefKey() { 70 return PREF_KEY_VIDEO; 71 } 72 73 @Override 74 protected boolean isSwitchPrefEnabled() { 75 return mAmbientConfig.pulseOnPickupEnabled(mUserId); 76 } 77 78 @Override 79 public String getPreferenceKey() { 80 return mPickUpPrefKey; 81 } 82 83 @Override 84 public boolean onPreferenceChange(Preference preference, Object newValue) { 85 final boolean enabled = (boolean) newValue; 86 Settings.Secure.putInt(mContext.getContentResolver(), 87 SECURE_KEY, enabled ? ON : OFF); 88 return true; 89 } 90 91 @Override 92 public boolean canHandleClicks() { 93 return mAmbientConfig.pulseOnPickupCanBeModified(mUserId); 94 } 95 96 @Override 97 public ResultPayload getResultPayload() { 98 final Intent intent = DatabaseIndexingUtils.buildSubsettingIntent(mContext, 99 PickupGestureSettings.class.getName(), mPickUpPrefKey, 100 mContext.getString(R.string.display_settings)); 101 102 return new InlineSwitchPayload(SECURE_KEY, ResultPayload.SettingsSource.SECURE, 103 ON /* onValue */, intent, isAvailable(), ON /* defaultValue */); 104 } 105 } 106