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.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.text.TextUtils; 27 import android.util.Log; 28 29 import androidx.preference.Preference; 30 31 import com.android.internal.annotations.VisibleForTesting; 32 import com.android.settings.R; 33 import com.android.settings.core.BasePreferenceController; 34 import com.android.settings.core.PreferenceControllerMixin; 35 import com.android.settings.flags.Flags; 36 37 import java.net.URISyntaxException; 38 39 /** 40 * A controller for the Settings button that launches "time feedback". The intent launches is 41 * configured with an Intent URI. 42 */ 43 public class TimeFeedbackPreferenceController 44 extends BasePreferenceController 45 implements PreferenceControllerMixin { 46 47 private static final String TAG = "TimeFeedbackController"; 48 49 private final PackageManager mPackageManager; 50 private final String mIntentUri; 51 TimeFeedbackPreferenceController(Context context, String preferenceKey)52 public TimeFeedbackPreferenceController(Context context, String preferenceKey) { 53 this(context, context.getPackageManager(), preferenceKey, context.getResources().getString( 54 R.string.config_time_feedback_intent_uri)); 55 } 56 57 @VisibleForTesting TimeFeedbackPreferenceController(Context context, PackageManager packageManager, String preferenceKey, String intentUri)58 TimeFeedbackPreferenceController(Context context, PackageManager packageManager, 59 String preferenceKey, String intentUri) { 60 super(context, preferenceKey); 61 mPackageManager = packageManager; 62 mIntentUri = intentUri; 63 } 64 65 /** 66 * Registers this controller with a category controller so that the category can be optionally 67 * displayed, i.e. if all the child controllers are not available, the category heading won't be 68 * available. 69 */ registerWithOptionalCategoryController( TimeFeedbackPreferenceCategoryController categoryController)70 public void registerWithOptionalCategoryController( 71 TimeFeedbackPreferenceCategoryController categoryController) { 72 categoryController.addChildController(this); 73 } 74 75 @Override getAvailabilityStatus()76 public int getAvailabilityStatus() { 77 if (!Flags.datetimeFeedback() || TextUtils.isEmpty(mIntentUri)) { 78 return UNSUPPORTED_ON_DEVICE; 79 } else if (!isTimeFeedbackTargetAvailable()) { 80 return CONDITIONALLY_UNAVAILABLE; 81 } 82 return AVAILABLE; 83 } 84 85 @Override handlePreferenceTreeClick(Preference preference)86 public boolean handlePreferenceTreeClick(Preference preference) { 87 if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) { 88 return super.handlePreferenceTreeClick(preference); 89 } 90 91 // Don't allow a monkey user to launch feedback 92 if (ActivityManager.isUserAMonkey()) { 93 return true; 94 } 95 96 try { 97 Intent intent = Intent.parseUri(mIntentUri, URI_INTENT_SCHEME); 98 mContext.startActivity(intent); 99 return true; 100 } catch (URISyntaxException e) { 101 Log.e(TAG, "Bad intent configuration: " + mIntentUri, e); 102 return false; 103 } 104 } 105 isTimeFeedbackTargetAvailable()106 private boolean isTimeFeedbackTargetAvailable() { 107 Intent intent; 108 try { 109 intent = Intent.parseUri(mIntentUri, URI_INTENT_SCHEME); 110 } catch (URISyntaxException e) { 111 Log.e(TAG, "Bad intent configuration: " + mIntentUri, e); 112 return false; 113 } 114 ComponentName resolvedActivity = intent.resolveActivity(mPackageManager); 115 116 if (resolvedActivity == null) { 117 Log.w(TAG, "No valid target for the time feedback intent: " + intent); 118 return false; 119 } 120 return true; 121 } 122 } 123