1 /*
2  * Copyright (C) 2021 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;
18 
19 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY;
20 
21 import android.content.res.Configuration;
22 import android.os.Bundle;
23 import android.util.Log;
24 import android.view.WindowManager;
25 
26 import androidx.collection.ArraySet;
27 import androidx.fragment.app.FragmentActivity;
28 import androidx.fragment.app.FragmentTransaction;
29 import androidx.lifecycle.ViewModelProvider;
30 
31 import com.android.car.carlauncher.homescreen.HomeCardModule;
32 
33 import java.util.Set;
34 
35 /**
36  * Launcher activity that shows only the control bar fragment.
37  */
38 public class ControlBarActivity extends FragmentActivity {
39     private static final String TAG = "CarLauncher";
40     private static final boolean DEBUG = false;
41 
42     private Set<HomeCardModule> mHomeCardModules;
43 
44     @Override
onCreate(Bundle savedInstanceState)45     protected void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47 
48         // Setting as trusted overlay to let touches pass through.
49         getWindow().addPrivateFlags(PRIVATE_FLAG_TRUSTED_OVERLAY);
50         // To pass touches to the underneath task.
51         getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
52 
53         setContentView(R.layout.control_bar_container);
54         initializeCards();
55     }
56 
57     @Override
onConfigurationChanged(Configuration newConfig)58     public void onConfigurationChanged(Configuration newConfig) {
59         super.onConfigurationChanged(newConfig);
60         initializeCards();
61     }
62 
initializeCards()63     private void initializeCards() {
64         if (mHomeCardModules == null) {
65             mHomeCardModules = new ArraySet<>();
66             for (String providerClassName : getResources().getStringArray(
67                     R.array.config_homeCardModuleClasses)) {
68                 try {
69                     long reflectionStartTime = System.currentTimeMillis();
70                     HomeCardModule cardModule = (HomeCardModule) Class.forName(
71                             providerClassName).newInstance();
72                     cardModule.setViewModelProvider(new ViewModelProvider(/* owner= */this));
73                     mHomeCardModules.add(cardModule);
74                     if (DEBUG) {
75                         long reflectionTime = System.currentTimeMillis() - reflectionStartTime;
76                         Log.d(TAG, "Initialization of HomeCardModule class " + providerClassName
77                                 + " took " + reflectionTime + " ms");
78                     }
79                 } catch (IllegalAccessException | InstantiationException
80                         | ClassNotFoundException e) {
81                     Log.w(TAG, "Unable to create HomeCardProvider class " + providerClassName, e);
82                 }
83             }
84         }
85         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
86         for (HomeCardModule cardModule : mHomeCardModules) {
87             transaction.replace(cardModule.getCardResId(), cardModule.getCardView().getFragment());
88         }
89         transaction.commitNow();
90     }
91 }
92