• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.settings.applications.appinfo;
18 
19 import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
20 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21 
22 import static com.android.car.settings.common.ActionButtonsPreference.ActionButtons.BUTTON1;
23 
24 import android.car.drivingstate.CarUxRestrictions;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.pm.PackageManager;
28 import android.os.UserHandle;
29 
30 import com.android.car.settings.R;
31 import com.android.car.settings.common.ActionButtonInfo;
32 import com.android.car.settings.common.ActionButtonsPreference;
33 import com.android.car.settings.common.FragmentController;
34 import com.android.car.settings.common.Logger;
35 import com.android.car.settings.common.PreferenceController;
36 
37 /**
38  * A PreferenceController handling the logic for the action button on the aspect ratio page.
39  */
40 public class AppAspectRatioActionButtonsPreferenceController extends
41         PreferenceController<ActionButtonsPreference> {
42     private static final Logger LOG = new Logger(
43             AppAspectRatioActionButtonsPreferenceController.class);
44     private PackageManager mPackageManager;
45     private String mPackageName;
46     private int mUserId;
47     private ActionButtonInfo mOpenApplicationButton;
48 
AppAspectRatioActionButtonsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)49     public AppAspectRatioActionButtonsPreferenceController(Context context, String preferenceKey,
50             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
51         super(context, preferenceKey, fragmentController, uxRestrictions);
52         mUserId = UserHandle.myUserId();
53         mPackageManager = context.getPackageManager();
54     }
55 
56     @Override
getPreferenceType()57     protected Class<ActionButtonsPreference> getPreferenceType() {
58         return ActionButtonsPreference.class;
59     }
60 
61     /**
62      * Set the packageName, which is used to perform actions on a particular package.
63      */
setPackageName(String packageName)64     public AppAspectRatioActionButtonsPreferenceController setPackageName(String packageName) {
65         mPackageName = packageName;
66         return this;
67     }
68 
69     @Override
checkInitialized()70     protected void checkInitialized() {
71         if (mPackageName == null) {
72             throw new IllegalStateException(
73                     "PackageName should be set before calling this function");
74         }
75     }
76 
77     @Override
onCreateInternal()78     protected void onCreateInternal() {
79         mOpenApplicationButton = getPreference().getButton(BUTTON1);
80         mOpenApplicationButton
81                 .setText(R.string.aspect_ratio_action_button)
82                 .setIcon(R.drawable.ic_open)
83                 .setOnClickListener(i -> launchApplication());
84     }
85 
launchApplication()86     private void launchApplication() {
87         Intent launchIntent = mPackageManager.getLaunchIntentForPackage(mPackageName)
88                 .addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
89         if (launchIntent != null) {
90             getContext().startActivityAsUser(launchIntent, new UserHandle(mUserId));
91         }
92     }
93 }
94