1 /* 2 * Copyright (C) 2015 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 android.assist.service; 18 19 import static android.service.voice.VoiceInteractionSession.SHOW_WITH_ASSIST; 20 import static android.service.voice.VoiceInteractionSession.SHOW_WITH_SCREENSHOT; 21 22 import android.assist.common.Utils; 23 import android.content.BroadcastReceiver; 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.IntentFilter; 28 import android.os.Bundle; 29 import android.service.voice.VoiceInteractionService; 30 import android.service.voice.VoiceInteractionSession; 31 import android.util.Log; 32 33 import java.lang.Exception; 34 import java.lang.Override; 35 import java.util.concurrent.CountDownLatch; 36 import java.util.concurrent.TimeUnit; 37 38 public class MainInteractionService extends VoiceInteractionService { 39 static final String TAG = "MainInteractionService"; 40 private Intent mIntent; 41 private boolean mReady = false; 42 private BroadcastReceiver mBroadcastReceiver, mResumeReceiver; 43 private CountDownLatch mResumeLatch; 44 45 @Override 46 public void onCreate() { 47 super.onCreate(); 48 Log.i(TAG, "onCreate received"); 49 } 50 51 @Override 52 public void onReady() { 53 super.onReady(); 54 Log.i(TAG, "onReady received"); 55 mReady = true; 56 } 57 58 @Override 59 public int onStartCommand(Intent intent, int flags, int startId) { 60 Log.i(TAG, "onStartCommand received - intent: " + intent); 61 mIntent = intent; 62 maybeStart(); 63 return START_NOT_STICKY; 64 } 65 66 private void maybeStart() { 67 if (mIntent == null || !mReady) { 68 Log.wtf(TAG, "Can't start session because either intent is null or onReady() " 69 + "has not been called yet. mIntent = " + mIntent + ", mReady = " + mReady); 70 } else { 71 if (isActiveService(this, new ComponentName(this, getClass()))) { 72 if (mIntent.getBooleanExtra(Utils.EXTRA_REGISTER_RECEIVER, false)) { 73 mResumeLatch = new CountDownLatch(1); 74 if (mBroadcastReceiver == null) { 75 mBroadcastReceiver = new MainInteractionServiceBroadcastReceiver(); 76 IntentFilter filter = new IntentFilter(); 77 filter.addAction(Utils.BROADCAST_INTENT_START_ASSIST); 78 registerReceiver(mBroadcastReceiver, filter); 79 Log.i(TAG, "Registered receiver to start session later"); 80 81 IntentFilter resumeFilter = new IntentFilter(Utils.APP_3P_HASRESUMED); 82 mResumeReceiver = new MainServiceAppResumeReceiver(); 83 registerReceiver(mResumeReceiver, resumeFilter); 84 Log.i(TAG, "Registered receiver for resuming activity"); 85 } 86 sendBroadcast(new Intent(Utils.ASSIST_RECEIVER_REGISTERED)); 87 } else { 88 Log.i(TAG, "Yay! about to start session"); 89 Bundle bundle = new Bundle(); 90 bundle.putString(Utils.TESTCASE_TYPE, 91 mIntent.getStringExtra(Utils.TESTCASE_TYPE)); 92 showSession(bundle, VoiceInteractionSession.SHOW_WITH_ASSIST | 93 VoiceInteractionSession.SHOW_WITH_SCREENSHOT); 94 } 95 } else { 96 Log.wtf(TAG, "**** Not starting MainInteractionService because" + 97 " it is not set as the current voice interaction service"); 98 } 99 } 100 } 101 102 private class MainInteractionServiceBroadcastReceiver extends BroadcastReceiver { 103 @Override 104 public void onReceive(Context context, Intent intent) { 105 Log.i(MainInteractionService.TAG, "Recieved broadcast to start session now."); 106 if (intent.getAction().equals(Utils.BROADCAST_INTENT_START_ASSIST)) { 107 String testCaseName = intent.getStringExtra(Utils.TESTCASE_TYPE); 108 Log.i(MainInteractionService.TAG, "trying to start 3p activity: " + testCaseName); 109 Bundle extras = intent.getExtras(); 110 if (extras == null) { 111 extras = new Bundle(); 112 } 113 if (testCaseName.equals(Utils.SCREENSHOT)) { 114 try { 115 // extra info to pass along to 3p activity. 116 117 Intent intent3p = new Intent(); 118 Log.i(TAG, "starting the 3p app again"); 119 intent3p.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 120 intent3p.setAction("android.intent.action.TEST_APP_" + testCaseName); 121 intent3p.setComponent(Utils.getTestAppComponent(testCaseName)); 122 intent3p.putExtras(extras); 123 startActivity(intent3p); 124 if (!MainInteractionService.this.mResumeLatch 125 .await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) { 126 Log.i(TAG, "waited for 3p activity to resume"); 127 } 128 } catch (Exception e) { 129 Log.i(TAG, "failed so reload 3p app: " + e.toString()); 130 } 131 } 132 extras.putString(Utils.TESTCASE_TYPE, mIntent.getStringExtra(Utils.TESTCASE_TYPE)); 133 MainInteractionService.this.showSession( 134 extras, SHOW_WITH_ASSIST | SHOW_WITH_SCREENSHOT); 135 } 136 } 137 } 138 139 private class MainServiceAppResumeReceiver extends BroadcastReceiver { 140 @Override 141 public void onReceive(Context context, Intent intent) { 142 if (intent.getAction().equals(Utils.APP_3P_HASRESUMED)) { 143 Log.i(MainInteractionService.TAG, 144 "3p activity has resumed in this new receiver"); 145 if (MainInteractionService.this.mResumeLatch != null) { 146 MainInteractionService.this.mResumeLatch.countDown(); 147 } else { 148 Log.i(TAG, "mResumeLatch was null"); 149 } 150 } 151 } 152 } 153 154 @Override 155 public void onDestroy() { 156 if (mBroadcastReceiver != null) { 157 unregisterReceiver(mBroadcastReceiver); 158 } 159 } 160 }