• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.app.ActivityOptions;
20 import android.content.Intent;
21 import android.content.res.Configuration;
22 import android.os.Bundle;
23 import android.util.Log;
24 import android.view.View;
25 
26 import androidx.annotation.NonNull;
27 import androidx.collection.ArraySet;
28 import androidx.fragment.app.FragmentActivity;
29 import androidx.fragment.app.FragmentTransaction;
30 import androidx.lifecycle.ViewModelProvider;
31 
32 import com.android.car.carlauncher.homescreen.HomeCardModule;
33 import com.android.car.carlauncher.homescreen.audio.IntentHandler;
34 import com.android.car.carlauncher.homescreen.audio.MediaLaunchHandler;
35 import com.android.car.carlauncher.homescreen.audio.dialer.InCallIntentRouter;
36 import com.android.car.carlauncher.homescreen.audio.media.MediaLaunchRouter;
37 import com.android.car.media.common.source.MediaSource;
38 
39 import java.util.Set;
40 
41 /**
42  * Launcher activity that shows only the control bar fragment.
43  */
44 public class ControlBarActivity extends FragmentActivity {
45     private static final String TAG = "CarLauncher";
46     private static final boolean DEBUG = false;
47 
48     private Set<HomeCardModule> mHomeCardModules;
49 
50     private final IntentHandler mIntentHandler = new IntentHandler() {
51         @Override
52         public void handleIntent(Intent intent) {
53             if (intent != null) {
54                 ActivityOptions options = ActivityOptions.makeBasic();
55                 startActivity(intent, options.toBundle());
56             }
57         }
58     };
59 
60     // Used instead of IntentHandler because media apps may provide a PendingIntent instead
61     private final MediaLaunchHandler mMediaMediaLaunchHandler = new MediaLaunchHandler() {
62         @Override
63         public void handleLaunchMedia(@NonNull MediaSource mediaSource) {
64             if (DEBUG) {
65                 Log.d(TAG, "Launching media source " + mediaSource);
66             }
67             mediaSource.launchActivity(ControlBarActivity.this, ActivityOptions.makeBasic());
68         }
69     };
70 
71     @Override
onCreate(Bundle savedInstanceState)72     protected void onCreate(Bundle savedInstanceState) {
73         super.onCreate(savedInstanceState);
74         getTheme().applyStyle(R.style.CarLauncherActivityThemeOverlay, true);
75 
76         setContentView(R.layout.control_bar_container);
77         initializeCards();
78 
79         MediaLaunchRouter.getInstance().registerMediaLaunchHandler(mMediaMediaLaunchHandler);
80         InCallIntentRouter.getInstance().registerInCallIntentHandler(mIntentHandler);
81     }
82 
83     @Override
onConfigurationChanged(Configuration newConfig)84     public void onConfigurationChanged(Configuration newConfig) {
85         super.onConfigurationChanged(newConfig);
86         initializeCards();
87     }
88 
initializeCards()89     private void initializeCards() {
90         if (mHomeCardModules == null) {
91             mHomeCardModules = new ArraySet<>();
92             for (String providerClassName : getResources().getStringArray(
93                     R.array.config_homeCardModuleClasses)) {
94                 try {
95                     long reflectionStartTime = System.currentTimeMillis();
96                     HomeCardModule cardModule = (HomeCardModule)
97                             Class.forName(providerClassName).newInstance();
98                     if (Flags.mediaCardFullscreen()) {
99                         if (cardModule.getCardResId() == R.id.top_card) {
100                             findViewById(R.id.top_card).setVisibility(View.GONE);
101                         }
102                     }
103                     cardModule.setViewModelProvider(new ViewModelProvider(/* owner= */this));
104                     mHomeCardModules.add(cardModule);
105                     if (DEBUG) {
106                         long reflectionTime = System.currentTimeMillis() - reflectionStartTime;
107                         Log.d(TAG, "Initialization of HomeCardModule class " + providerClassName
108                                 + " took " + reflectionTime + " ms");
109                     }
110                 } catch (IllegalAccessException | InstantiationException
111                          | ClassNotFoundException e) {
112                     Log.w(TAG, "Unable to create HomeCardProvider class " + providerClassName, e);
113                 }
114             }
115         }
116         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
117         for (HomeCardModule cardModule : mHomeCardModules) {
118             transaction.replace(cardModule.getCardResId(), cardModule.getCardView().getFragment());
119         }
120         transaction.commitNow();
121     }
122 }
123