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.portraitlauncher.calmmode;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.graphics.Insets;
24 import android.os.Build;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.view.View;
28 import android.view.WindowInsets;
29 
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 import androidx.fragment.app.FragmentActivity;
33 
34 import com.android.car.portraitlauncher.R;
35 
36 // TODO(b/329713280): Add portrait-specific tests
37 public class PortraitCalmModeActivity extends FragmentActivity {
38     private static final String TAG = PortraitCalmModeActivity.class.getSimpleName();
39     private static final boolean DEBUG = Build.isDebuggable();
40     private static final String APP_PACKAGE_NAME = "com.android.car.portraitlauncher";
41     public static final String INTENT_ACTION_DISMISS_CALM_MODE =
42             APP_PACKAGE_NAME + ".ACTION_DISMISS_CALM_MODE";
43     public static final Intent INTENT_DISMISS_CALM_MODE;
44 
45     static {
46         INTENT_DISMISS_CALM_MODE = new Intent();
47         INTENT_DISMISS_CALM_MODE.setPackage(APP_PACKAGE_NAME);
48         INTENT_DISMISS_CALM_MODE.setAction(INTENT_ACTION_DISMISS_CALM_MODE);
49     }
50 
51     /** Sends a broadcast intent to end Calm mode
52      * @param context used for sending broadcast
53      */
dismissCalmMode(@onNull Context context)54     public static void dismissCalmMode(@NonNull Context context) {
55         context.sendBroadcast(INTENT_DISMISS_CALM_MODE);
56     }
57 
58     private final BroadcastReceiver mDismissReceiver = new BroadcastReceiver() {
59         @Override
60         public void onReceive(Context context, Intent intent) {
61             if (DEBUG) {
62                 Log.v(TAG, "Received broadcast:" + " intent=" + intent
63                         + ", extras=" + intent.getExtras() + ", user= " + context.getUser());
64             }
65             finish();
66         }
67     };
68 
69     private final View.OnApplyWindowInsetsListener mOnApplyWindowInsetsListener =
70             (v, insets) -> {
71                 int insetTypes = WindowInsets.Type.systemBars();
72                 Insets appliedInsets = insets.getInsets(insetTypes);
73                 v.setPadding(
74                         appliedInsets.left, appliedInsets.top,
75                         appliedInsets.right, /* bottom= */ 0);
76                 return insets.inset(appliedInsets);
77             };
78 
79     @Override
onCreate(@ullable Bundle savedInstanceState)80     protected void onCreate(@Nullable Bundle savedInstanceState) {
81         super.onCreate(savedInstanceState);
82         getWindow().setDecorFitsSystemWindows(/* decorFitsSystemWindows= */ false);
83         getWindow()
84                 .getDecorView()
85                 .getRootView()
86                 .setOnApplyWindowInsetsListener(mOnApplyWindowInsetsListener);
87         registerBroadcastReceiver();
88         setContentView(R.layout.calm_mode_activity);
89     }
90 
registerBroadcastReceiver()91     private void registerBroadcastReceiver() {
92         IntentFilter filter = new IntentFilter();
93         filter.addAction(PortraitCalmModeActivity.INTENT_ACTION_DISMISS_CALM_MODE);
94         registerReceiver(mDismissReceiver, filter, /* broadcastPermission= */ null,
95                 /* scheduler= */ null, Context.RECEIVER_NOT_EXPORTED);
96     }
97 
98     @Override
onDestroy()99     protected void onDestroy() {
100         super.onDestroy();
101         unregisterReceiver(mDismissReceiver);
102     }
103 
104 }
105