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 package com.android.car.voicecontrol;
17 
18 import android.app.Activity;
19 import android.content.Intent;
20 import android.content.pm.PackageManager;
21 import android.os.Bundle;
22 import android.provider.Settings;
23 import android.view.View;
24 import android.widget.Button;
25 import android.widget.Toast;
26 
27 import androidx.annotation.Nullable;
28 
29 import com.android.car.ui.core.CarUi;
30 import com.android.car.ui.toolbar.Toolbar;
31 import com.android.car.ui.toolbar.ToolbarController;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 /**
37  * Sample sign-in or setup activity. This represents a UI this voice interaction service would use
38  * to configure the service. If this service is not provisioned yet when it gets selected as
39  * default, this UI will be shown. If the user exists the setup before it is complete, a
40  * notification will be displayed, reminding the user to complete the setup.
41  */
42 public class SignInActivity extends Activity {
43     private static final String TAG = "Mica.SignInActivity";
44     private static final int REQUEST_CODE = 1;
45 
46     private InteractionServiceClient mInteractionService;
47     private Button mSignInButton;
48     private Button mPermissionButton;
49     private Button mNotifListenerButton;
50 
51     @Override
onCreate(@ullable Bundle savedInstanceState)52     protected void onCreate(@Nullable Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         setContentView(R.layout.sign_in_activity);
55 
56         ToolbarController toolbar = CarUi.requireToolbar(this);
57         toolbar.setTitle(R.string.sign_in_title);
58         toolbar.setState(Toolbar.State.SUBPAGE);
59 
60         mSignInButton = findViewById(R.id.sign_in_button);
61         mSignInButton.setOnClickListener(v -> onSignInClicked());
62         mPermissionButton = findViewById(R.id.permissions_button);
63         mPermissionButton.setOnClickListener(v -> onRequestPermissions());
64         mNotifListenerButton = findViewById(R.id.notifications_button);
65         mNotifListenerButton.setOnClickListener(v -> onRequestNotificationListenerAccess());
66         mInteractionService = new InteractionServiceClient(this) {
67             @Override
68             void onConnected() {
69                 updateSetupStatus();
70             }
71 
72             @Override
73             void onSetupChanged() {
74                 updateSetupStatus();
75             }
76         };
77     }
78 
79     @Override
onResume()80     protected void onResume() {
81         super.onResume();
82         mInteractionService.connect();
83     }
84 
85     @Override
onPause()86     protected void onPause() {
87         mInteractionService.disconnect();
88         super.onPause();
89     }
90 
updateSetupStatus()91     private void updateSetupStatus() {
92         if (!mInteractionService.isConnected()) {
93             // Wait until we are connected
94             return;
95         }
96         if (mInteractionService.isSetupComplete()) {
97             finish();
98         } else {
99             mSignInButton.setVisibility(!mInteractionService.hasUsername()
100                     ? View.VISIBLE : View.GONE);
101             mPermissionButton.setVisibility(!mInteractionService.hasAllPermissions()
102                     ? View.VISIBLE : View.GONE);
103             mNotifListenerButton.setVisibility(!mInteractionService.isNotificationListener()
104                     ? View.VISIBLE : View.GONE);
105         }
106     }
107 
onSignInClicked()108     private void onSignInClicked() {
109         // This is just a fake implementation of sign-in. Complete implementation of authentication
110         // flow is outside the scope of this sample application.
111         mInteractionService.setUsername(getString(R.string.sign_in_fake_username));
112     }
113 
onRequestPermissions()114     private void onRequestPermissions() {
115         requestPermissions(InteractionService.REQUIRED_PERMISSIONS.toArray(new String[0]),
116                 REQUEST_CODE);
117     }
118 
119     @Override
onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)120     public void onRequestPermissionsResult(int requestCode, String[] permissions,
121             int[] grantResults) {
122         if (requestCode != REQUEST_CODE) {
123             return;
124         }
125         List<String> missingPermissions = new ArrayList<>();
126         for (int i = 0; i < grantResults.length; i++) {
127             if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
128                 missingPermissions.add(permissions[i]);
129             }
130         }
131         if (!missingPermissions.isEmpty()) {
132             Toast.makeText(this, "The following permissions have not been granted: "
133                     + missingPermissions, Toast.LENGTH_SHORT).show();
134             return;
135         }
136         mInteractionService.notifySetupChanged();
137     }
138 
onRequestNotificationListenerAccess()139     private void onRequestNotificationListenerAccess() {
140         Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
141         intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
142         startActivity(intent);
143     }
144 }
145