1 /* 2 * Copyright (C) 2023 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 com.android.car.carlauncher.AppGridFragment.MODE_INTENT_EXTRA; 20 21 import android.content.Intent; 22 import android.os.Bundle; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 import androidx.appcompat.app.AppCompatActivity; 27 import androidx.fragment.app.Fragment; 28 29 import com.android.car.carlauncher.AppGridFragment.Mode; 30 import com.android.car.ui.core.CarUi; 31 import com.android.car.ui.toolbar.MenuItem; 32 import com.android.car.ui.toolbar.NavButtonMode; 33 import com.android.car.ui.toolbar.ToolbarController; 34 35 import java.util.Collections; 36 37 /** 38 * Launcher activity that shows a grid of apps. 39 */ 40 public class AppGridActivity extends AppCompatActivity { 41 private static final String TAG = "AppGridActivity"; 42 boolean mShowToolbar = false; 43 boolean mShowAllApps = true; 44 private static final boolean DEBUG_BUILD = false; 45 46 @Override onCreate(@ullable Bundle savedInstanceState)47 protected void onCreate(@Nullable Bundle savedInstanceState) { 48 // TODO (b/267548246) deprecate toolbar and find another way to hide debug apps 49 if (mShowToolbar) { 50 setTheme(R.style.Theme_Launcher_AppGridActivity); 51 } else { 52 setTheme(R.style.Theme_Launcher_AppGridActivity_NoToolbar); 53 } 54 super.onCreate(savedInstanceState); 55 setContentView(R.layout.app_grid_container_activity); 56 57 if (mShowToolbar) { 58 ToolbarController toolbar = CarUi.requireToolbar(this); 59 toolbar.setNavButtonMode(NavButtonMode.CLOSE); 60 if (DEBUG_BUILD) { 61 toolbar.setMenuItems(Collections.singletonList(MenuItem.builder(this) 62 .setDisplayBehavior(MenuItem.DisplayBehavior.NEVER) 63 .setTitle(R.string.hide_debug_apps) 64 .setOnClickListener(i -> { 65 mShowAllApps = !mShowAllApps; 66 i.setTitle(mShowAllApps 67 ? R.string.hide_debug_apps 68 : R.string.show_debug_apps); 69 }) 70 .build())); 71 } 72 } 73 getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, 74 AppGridFragment.newInstance(parseMode(getIntent()))).commit(); 75 } 76 77 @Override onNewIntent(Intent intent)78 protected void onNewIntent(Intent intent) { 79 super.onNewIntent(intent); 80 setIntent(intent); 81 Mode mode = parseMode(intent); 82 setTitle(mode.getTitleStringId()); 83 if (mShowToolbar) { 84 CarUi.requireToolbar(this).setTitle(mode.getTitleStringId()); 85 } 86 Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainer); 87 if (fragment instanceof AppGridFragment) { 88 ((AppGridFragment) fragment).updateMode(mode); 89 } 90 } 91 92 /** 93 * Note: This activity is exported, meaning that it might receive intents from any source. 94 * Intent data parsing must be extra careful. 95 */ 96 @NonNull parseMode(@ullable Intent intent)97 private Mode parseMode(@Nullable Intent intent) { 98 String mode = intent != null ? intent.getStringExtra(MODE_INTENT_EXTRA) : null; 99 try { 100 return mode != null ? Mode.valueOf(mode) : Mode.ALL_APPS; 101 } catch (IllegalArgumentException e) { 102 throw new IllegalArgumentException("Received invalid mode: " + mode, e); 103 } 104 } 105 106 } 107