1 /* 2 * Copyright (C) 2024 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.datetime; 18 19 import static android.content.Intent.URI_INTENT_SCHEME; 20 21 import android.app.ActivityManager; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.text.TextUtils; 25 26 import androidx.preference.Preference; 27 28 import com.android.internal.annotations.VisibleForTesting; 29 import com.android.settings.R; 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settings.core.PreferenceControllerMixin; 32 33 import java.net.URISyntaxException; 34 35 /** 36 * A controller for the Settings button that launches "time feedback". The intent launches is 37 * configured with an Intent URI. 38 */ 39 public class TimeFeedbackPreferenceController 40 extends BasePreferenceController 41 implements PreferenceControllerMixin { 42 43 private final String mIntentUri; 44 private final int mAvailabilityStatus; 45 TimeFeedbackPreferenceController(Context context, String preferenceKey)46 public TimeFeedbackPreferenceController(Context context, String preferenceKey) { 47 this(context, preferenceKey, context.getResources().getString( 48 R.string.config_time_feedback_intent_uri)); 49 } 50 51 @VisibleForTesting TimeFeedbackPreferenceController(Context context, String preferenceKey, String intentUri)52 TimeFeedbackPreferenceController(Context context, String preferenceKey, String intentUri) { 53 super(context, preferenceKey); 54 mIntentUri = intentUri; 55 mAvailabilityStatus = TextUtils.isEmpty(mIntentUri) ? UNSUPPORTED_ON_DEVICE : AVAILABLE; 56 } 57 58 /** 59 * Registers this controller with a category controller so that the category can be optionally 60 * displayed, i.e. if all the child controllers are not available, the category heading won't be 61 * available. 62 */ registerWithOptionalCategoryController( TimeFeedbackPreferenceCategoryController categoryController)63 public void registerWithOptionalCategoryController( 64 TimeFeedbackPreferenceCategoryController categoryController) { 65 categoryController.addChildController(this); 66 } 67 68 @Override getAvailabilityStatus()69 public int getAvailabilityStatus() { 70 if (!TimeFeedbackLaunchUtils.isFeedbackFeatureSupported()) { 71 return UNSUPPORTED_ON_DEVICE; 72 } 73 return mAvailabilityStatus; 74 } 75 76 @Override handlePreferenceTreeClick(Preference preference)77 public boolean handlePreferenceTreeClick(Preference preference) { 78 if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) { 79 return super.handlePreferenceTreeClick(preference); 80 } 81 82 // Don't allow a monkey user to launch feedback 83 if (ActivityManager.isUserAMonkey()) { 84 return true; 85 } 86 87 try { 88 Intent intent = Intent.parseUri(mIntentUri, URI_INTENT_SCHEME); 89 mContext.startActivity(intent); 90 return true; 91 } catch (URISyntaxException e) { 92 throw new IllegalArgumentException("Bad intent configuration: " + mIntentUri, e); 93 } 94 } 95 } 96