1 /*
2  * Copyright (C) 2017 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.cts.devicepolicy.assistapp;
17 
18 import android.app.Activity;
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.Bundle;
24 import android.service.voice.VoiceInteractionService;
25 import android.util.Log;
26 
27 import static android.service.voice.VoiceInteractionSession.SHOW_WITH_ASSIST;
28 import static android.service.voice.VoiceInteractionSession.SHOW_WITH_SCREENSHOT;
29 
30 public class MyInteractionService extends VoiceInteractionService {
31     private static final String TAG = "DevicePolicyAssistTest";
32     private static final String ACTION_CHECK_IS_READY = "voice_interaction_service.is_ready";
33     private static final String ACTION_SHOW_SESSION = "voice_interaction_service.show_session";
34     private AssistReceiver mReceiver;
35     private boolean mReady;
36 
37     @Override
onCreate()38     public void onCreate() {
39         super.onCreate();
40         mReceiver = new AssistReceiver();
41         IntentFilter intentFilter = new IntentFilter();
42         intentFilter.addAction(ACTION_CHECK_IS_READY);
43         intentFilter.addAction(ACTION_SHOW_SESSION);
44         registerReceiver(mReceiver, intentFilter);
45     }
46 
47     @Override
onReady()48     public void onReady() {
49         super.onReady();
50         Log.d(TAG, "onReady() called");
51         mReady = true;
52     }
53 
54     @Override
onDestroy()55     public void onDestroy() {
56         super.onDestroy();
57         Log.d(TAG, "onDestroy() called");
58         unregisterReceiver(mReceiver);
59         mReceiver = null;
60     }
61 
62     private class AssistReceiver extends BroadcastReceiver {
63 
64         @Override
onReceive(Context context, Intent intent)65         public void onReceive(Context context, Intent intent) {
66             Log.d(TAG, "AssistReceiver: " + intent + " mReady: " + mReady);
67             if (ACTION_CHECK_IS_READY.equals(intent.getAction())) {
68                 setResultCode(mReady ? Activity.RESULT_OK : Activity.RESULT_CANCELED);
69             } else if (ACTION_SHOW_SESSION.equals(intent.getAction())) {
70                 showSession(
71                         new Bundle(),
72                         SHOW_WITH_ASSIST | SHOW_WITH_SCREENSHOT);
73             }
74         }
75     }
76 }
77