1 /* 2 * Copyright (C) 2022 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 android.car.app; 18 19 import static com.android.car.internal.util.VersionUtils.assertPlatformVersionAtLeastU; 20 21 import android.annotation.NonNull; 22 import android.annotation.SystemApi; 23 import android.car.annotation.ApiRequirements; 24 import android.content.Intent; 25 26 /** 27 * This class provides the required configuration to create a 28 * {@link ControlledRemoteCarTaskView}. 29 * @hide 30 */ 31 @SystemApi 32 public final class ControlledRemoteCarTaskViewConfig { 33 private static final String TAG = ControlledRemoteCarTaskViewConfig.class.getSimpleName(); 34 35 final Intent mActivityIntent; 36 final boolean mShouldAutoRestartOnTaskRemoval; 37 final boolean mShouldCaptureGestures; 38 final boolean mShouldCaptureLongPress; 39 ControlledRemoteCarTaskViewConfig( Intent activityIntent, boolean shouldAutoRestartOnTaskRemoval, boolean shouldCaptureGestures, boolean shouldCaptureLongPress)40 private ControlledRemoteCarTaskViewConfig( 41 Intent activityIntent, 42 boolean shouldAutoRestartOnTaskRemoval, 43 boolean shouldCaptureGestures, 44 boolean shouldCaptureLongPress) { 45 mActivityIntent = activityIntent; 46 mShouldAutoRestartOnTaskRemoval = shouldAutoRestartOnTaskRemoval; 47 mShouldCaptureGestures = shouldCaptureGestures; 48 mShouldCaptureLongPress = shouldCaptureLongPress; 49 } 50 51 /** See {@link Builder#setActivityIntent(Intent)}. */ 52 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 53 minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0) 54 @NonNull getActivityIntent()55 public Intent getActivityIntent() { 56 assertPlatformVersionAtLeastU(); 57 return mActivityIntent; 58 } 59 60 /** See {@link Builder#setShouldAutoRestartOnTaskRemoval(boolean)}. */ 61 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 62 minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0) shouldAutoRestartOnCrash()63 public boolean shouldAutoRestartOnCrash() { 64 assertPlatformVersionAtLeastU(); 65 return mShouldAutoRestartOnTaskRemoval; 66 } 67 68 /** See {@link Builder#setShouldCaptureGestures(boolean)}. */ 69 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 70 minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0) shouldCaptureGestures()71 public boolean shouldCaptureGestures() { 72 assertPlatformVersionAtLeastU(); 73 return mShouldCaptureGestures; 74 } 75 76 /** See {@link Builder#setShouldCaptureLongPress(boolean)}. */ 77 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 78 minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0) shouldCaptureLongPress()79 public boolean shouldCaptureLongPress() { 80 assertPlatformVersionAtLeastU(); 81 return mShouldCaptureLongPress; 82 } 83 84 @Override toString()85 public String toString() { 86 return TAG + " {" 87 + "activityIntent=" + mActivityIntent 88 + ", mShouldAutoRestartOnTaskRemoval=" + mShouldAutoRestartOnTaskRemoval 89 + ", shouldCaptureGestures=" + mShouldCaptureGestures 90 + ", shouldCaptureLongPress=" + mShouldCaptureLongPress 91 + '}'; 92 } 93 94 /** 95 * A builder class for {@link ControlledRemoteCarTaskViewConfig}. 96 * @hide 97 */ 98 @SystemApi 99 public static final class Builder { 100 private Intent mActivityIntent; 101 private boolean mShouldAutoRestartOnCrash; 102 private boolean mShouldCaptureGestures; 103 private boolean mShouldCaptureLongPress; 104 Builder()105 public Builder() { 106 } 107 108 /** 109 * Sets the intent of the activity that is meant to be started in this {@link 110 * ControlledRemoteCarTaskView}. 111 * 112 * @param activityIntent the intent of the activity that is meant to be started in this 113 * task view. 114 */ 115 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 116 minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0) 117 @NonNull setActivityIntent(@onNull Intent activityIntent)118 public Builder setActivityIntent(@NonNull Intent activityIntent) { 119 assertPlatformVersionAtLeastU(); 120 mActivityIntent = activityIntent; 121 return this; 122 } 123 124 /** 125 * Sets the auto restart functionality. If set, the {@link ControlledRemoteCarTaskView} 126 * will restart the task by re-launching the intent set via {@link 127 * #setActivityIntent(Intent)} when the task is removed. 128 * 129 * The task might get removed because of multiple reasons like: 130 * <ul> 131 * <li>Due to memory pressure</li> 132 * <li>Due to the apk update</li> 133 * <li>etc.</li> 134 * </ul> 135 * 136 * @param shouldAutoRestartOnTaskRemoval denotes if the auto restart functionality should be 137 * enabled or not. 138 */ 139 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 140 minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0) 141 @NonNull setShouldAutoRestartOnTaskRemoval(boolean shouldAutoRestartOnTaskRemoval)142 public Builder setShouldAutoRestartOnTaskRemoval(boolean shouldAutoRestartOnTaskRemoval) { 143 assertPlatformVersionAtLeastU(); 144 mShouldAutoRestartOnCrash = shouldAutoRestartOnTaskRemoval; 145 return this; 146 } 147 148 /** 149 * Enables the swipe gesture capturing over {@link ControlledRemoteCarTaskView}. When 150 * enabled, the swipe gestures won't be sent to the embedded app and will instead be 151 * forwarded to the host activity. 152 * 153 * @param shouldCaptureGestures denotes if the swipe gesture capturing should be enabled or 154 * not. 155 */ 156 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 157 minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0) 158 @NonNull setShouldCaptureGestures(boolean shouldCaptureGestures)159 public Builder setShouldCaptureGestures(boolean shouldCaptureGestures) { 160 assertPlatformVersionAtLeastU(); 161 mShouldCaptureGestures = shouldCaptureGestures; 162 return this; 163 } 164 165 /** 166 * Enables the long press capturing over {@link ControlledRemoteCarTaskView}. When enabled, 167 * the long press won't be sent to the embedded app and will instead be sent to the listener 168 * specified via {@link 169 * ControlledRemoteCarTaskView#setOnLongClickListener(View.OnLongClickListener)}. 170 * 171 * <p>If disabled, the listener supplied via {@link 172 * ControlledRemoteCarTaskView#setOnLongClickListener(View.OnLongClickListener)} won't be 173 * called. 174 * 175 * @param shouldCaptureLongPress denotes if the long press capturing should be enabled or 176 * not. 177 */ 178 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 179 minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0) 180 @NonNull setShouldCaptureLongPress(boolean shouldCaptureLongPress)181 public Builder setShouldCaptureLongPress(boolean shouldCaptureLongPress) { 182 assertPlatformVersionAtLeastU(); 183 mShouldCaptureLongPress = shouldCaptureLongPress; 184 return this; 185 } 186 187 /** Creates the {@link ControlledRemoteCarTaskViewConfig} object. */ 188 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 189 minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0) 190 @NonNull build()191 public ControlledRemoteCarTaskViewConfig build() { 192 assertPlatformVersionAtLeastU(); 193 if (mActivityIntent == null) { 194 throw new IllegalArgumentException("mActivityIntent can't be null"); 195 } 196 return new ControlledRemoteCarTaskViewConfig( 197 mActivityIntent, mShouldAutoRestartOnCrash, mShouldCaptureGestures, 198 mShouldCaptureLongPress); 199 } 200 } 201 } 202