• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.ui.paintbooth.overlays;
18 
19 import android.os.Bundle;
20 import android.util.Log;
21 import android.widget.Toast;
22 
23 import androidx.appcompat.app.AppCompatActivity;
24 import androidx.preference.SwitchPreference;
25 
26 import com.android.car.ui.paintbooth.R;
27 import com.android.car.ui.preference.PreferenceFragment;
28 
29 import java.util.List;
30 import java.util.Map;
31 
32 /**
33  * Activity representing lists of RROs ppackage name as title and the corresponding target package
34  * name as the summary with a toggle switch to enable/disable the overlays.
35  */
36 public class OverlayActivity extends AppCompatActivity {
37     private static final String TAG = OverlayActivity.class.getSimpleName();
38 
39     @Override
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42 
43         // Display the fragment as the main content.
44         if (savedInstanceState == null) {
45             getSupportFragmentManager()
46                     .beginTransaction()
47                     .replace(android.R.id.content, new OverlayFragment())
48                     .commit();
49         }
50     }
51 
52     /** PreferenceFragmentCompat that sets the preference hierarchy from XML */
53     public static class OverlayFragment extends PreferenceFragment {
54         private OverlayManager mOverlayManager;
55 
56         @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)57         public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
58             try {
59                 mOverlayManager = OverlayManager.getInstance(getContext());
60                 setPreferencesFromResource(R.xml.preference_overlays, rootKey);
61 
62                 Map<String, List<OverlayManager.OverlayInfo>> overlays =
63                         OverlayManager.getInstance(getContext()).getOverlays();
64 
65                 for (String targetPackage : overlays.keySet()) {
66                     for (OverlayManager.OverlayInfo overlayPackage : overlays.get(targetPackage)) {
67                         SwitchPreference switchPreference = new SwitchPreference(getContext());
68                         switchPreference.setKey(overlayPackage.getPackageName());
69                         switchPreference.setTitle(overlayPackage.getPackageName());
70                         switchPreference.setSummary(targetPackage);
71                         switchPreference.setChecked(overlayPackage.isEnabled());
72 
73                         switchPreference.setOnPreferenceChangeListener((preference, newValue) -> {
74                             applyOverlay(overlayPackage.getPackageName(), (boolean) newValue);
75                             return true;
76                         });
77 
78                         getPreferenceScreen().addPreference(switchPreference);
79                     }
80                 }
81             } catch (Exception e) {
82                 Toast.makeText(getContext(), "Error: " + e.getMessage(),
83                         Toast.LENGTH_LONG).show();
84                 Log.e(TAG, "Can't load overlays: ", e);
85             }
86         }
87 
applyOverlay(String overlayPackage, boolean enableOverlay)88         private void applyOverlay(String overlayPackage, boolean enableOverlay) {
89             try {
90                 mOverlayManager.applyOverlay(overlayPackage, enableOverlay);
91             } catch (Exception e) {
92                 Toast.makeText(getContext(), "Error: " + e.getMessage(),
93                         Toast.LENGTH_LONG).show();
94                 Log.e(TAG, "Can't apply overlay: ", e);
95             }
96         }
97     }
98 }
99