1 /* 2 * Copyright (C) 2023 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.car.carlauncher.calmmode; 18 19 import android.app.ActivityOptions; 20 import android.app.PendingIntent; 21 import android.content.ComponentName; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.res.Resources; 26 import android.net.Uri; 27 import android.os.Build; 28 import android.util.Log; 29 30 import androidx.annotation.VisibleForTesting; 31 32 import com.android.car.carlauncher.Flags; 33 import com.android.car.carlauncher.R; 34 import com.android.car.qc.QCItem; 35 import com.android.car.qc.QCList; 36 import com.android.car.qc.QCRow; 37 import com.android.car.qc.provider.BaseQCProvider; 38 39 import java.util.Set; 40 import java.util.concurrent.atomic.AtomicInteger; 41 42 /** 43 * Remote Quick Control provider for Calm mode in CarLauncher. 44 */ 45 public class CalmModeQCProvider extends BaseQCProvider { 46 private static final boolean DEBUG = Build.IS_DEBUGGABLE; 47 public static final String AUTHORITY = "com.android.car.carlauncher.calmmode"; 48 private static final String TAG = CalmModeQCProvider.class.getSimpleName(); 49 50 private static final String CALM_MODE_SEGMENT = "calm_mode"; 51 // Start Uris 52 public static final Uri CALM_MODE_URI = new Uri.Builder().scheme( 53 ContentResolver.SCHEME_CONTENT) 54 .authority(AUTHORITY) 55 .appendPath(CALM_MODE_SEGMENT) 56 .build(); 57 private Set<String> mAllowListedPackages; 58 private Context mContext; 59 private AtomicInteger mPendingIntentRequestCode = new AtomicInteger(0); 60 @VisibleForTesting 61 QCItem mQCItem; 62 63 /** 64 * Returns a uri without its parameters (or null if the provided uri is null). 65 */ removeParameterFromUri(Uri uri)66 public static Uri removeParameterFromUri(Uri uri) { 67 return uri != null ? uri.buildUpon().clearQuery().build() : null; 68 } 69 70 @Override onCreate()71 public boolean onCreate() { 72 if (!Flags.calmMode()) { 73 return false; 74 } 75 boolean returnVal = super.onCreate(); 76 if (DEBUG) { 77 Log.v(TAG, "onCreate() returnVal " + returnVal); 78 } 79 mAllowListedPackages = Set.of(getContext().getResources().getStringArray( 80 R.array.launcher_qc_provider_package_allowlist)); 81 mContext = getContext(); 82 mQCItem = getQCItem(); 83 return returnVal; 84 } 85 86 @Override onBind(Uri uri)87 public QCItem onBind(Uri uri) { 88 if (!Flags.calmMode()) { 89 return null; 90 } 91 boolean isValidCalmModeURI = removeParameterFromUri(uri).equals(CALM_MODE_URI); 92 if (DEBUG) { 93 Log.v(TAG, "onBind() uri=" + uri + ", isValidCalmModeURI= " + isValidCalmModeURI); 94 } 95 96 if (!isValidCalmModeURI) { 97 throw new IllegalArgumentException("No QCItem found for uri: " + uri); 98 } 99 return mQCItem; 100 } 101 102 @Override getAllowlistedPackages()103 protected Set<String> getAllowlistedPackages() { 104 return mAllowListedPackages; 105 } 106 107 @VisibleForTesting getQCItem()108 QCItem getQCItem() { 109 Resources resources = mContext.getResources(); 110 ComponentName componentName = ComponentName.unflattenFromString( 111 resources.getString(R.string.config_calmMode_componentName)); 112 Intent intent = new Intent(); 113 intent.setComponent(componentName); 114 intent.putExtra(CalmModeStatsLogHelper.INTENT_EXTRA_CALM_MODE_LAUNCH_TYPE, 115 CalmModeStatsLogHelper.CalmModeLaunchType.QUICK_CONTROLS); 116 ActivityOptions activityOptions = ActivityOptions.makeBasic() 117 .setPendingIntentCreatorBackgroundActivityStartMode( 118 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED); 119 PendingIntent calmModeIntent = PendingIntent.getActivity(mContext, 120 mPendingIntentRequestCode.getAndAdd(1), intent, 121 PendingIntent.FLAG_IMMUTABLE, activityOptions.toBundle()); 122 123 QCRow calmModeRow = new QCRow.Builder() 124 .setTitle(mContext.getString(R.string.calm_mode_title)) 125 .setPrimaryAction(calmModeIntent) 126 .build(); 127 128 return new QCList.Builder().addRow(calmModeRow).build(); 129 } 130 } 131