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 17package com.googlecode.android_scripting.activity; 18 19import android.app.Activity; 20import android.app.Service; 21import android.content.Intent; 22import android.content.pm.ResolveInfo; 23import android.os.Bundle; 24import android.view.ContextMenu; 25import android.view.ContextMenu.ContextMenuInfo; 26import android.view.KeyEvent; 27import android.view.Menu; 28import android.view.View; 29 30import com.googlecode.android_scripting.BaseApplication; 31import com.googlecode.android_scripting.Constants; 32import com.googlecode.android_scripting.FutureActivityTaskExecutor; 33import com.googlecode.android_scripting.Log; 34import com.googlecode.android_scripting.future.FutureActivityTask; 35import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 36 37/** 38 * This {@link Activity} is launched by {@link RpcReceiver}s in order to perform operations that a 39 * {@link Service} is unable to do. For example: start another activity for result, show dialogs, 40 * etc. 41 * 42 */ 43public class FutureActivity extends Activity { 44 private FutureActivityTask<?> mTask; 45 46 @Override 47 protected void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 Log.v("FutureActivity created."); 50 int id = getIntent().getIntExtra(Constants.EXTRA_TASK_ID, 0); 51 if (id == 0) { 52 throw new RuntimeException("FutureActivityTask ID is not specified."); 53 } 54 FutureActivityTaskExecutor taskQueue = ((BaseApplication) getApplication()).getTaskExecutor(); 55 mTask = taskQueue.getTask(id); 56 if (mTask == null) { // TODO: (Robbie) This is now less of a kludge. Would still like to know 57 // what is happening. 58 Log.w("FutureActivity has no task!"); 59 try { 60 Intent intent = new Intent(Intent.ACTION_MAIN); // Should default to main of current app. 61 intent.addCategory(Intent.CATEGORY_LAUNCHER); 62 String packageName = getPackageName(); 63 for (ResolveInfo resolve : getPackageManager().queryIntentActivities(intent, 0)) { 64 if (resolve.activityInfo.packageName.equals(packageName)) { 65 intent.setClassName(packageName, resolve.activityInfo.name); 66 break; 67 } 68 } 69 startActivity(intent); 70 } catch (Exception e) { 71 Log.e("Can't find main activity."); 72 } 73 } else { 74 mTask.setActivity(this); 75 mTask.onCreate(); 76 } 77 } 78 79 @Override 80 protected void onStart() { 81 super.onStart(); 82 if (mTask != null) { 83 mTask.onStart(); 84 } 85 } 86 87 @Override 88 protected void onResume() { 89 super.onResume(); 90 if (mTask != null) { 91 mTask.onResume(); 92 } 93 } 94 95 @Override 96 protected void onPause() { 97 super.onPause(); 98 if (mTask != null) { 99 mTask.onPause(); 100 } 101 } 102 103 @Override 104 protected void onStop() { 105 super.onStop(); 106 if (mTask != null) { 107 mTask.onStop(); 108 } 109 } 110 111 @Override 112 protected void onDestroy() { 113 super.onDestroy(); 114 if (mTask != null) { 115 mTask.onDestroy(); 116 } 117 } 118 119 @Override 120 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 121 if (mTask != null) { 122 mTask.onCreateContextMenu(menu, v, menuInfo); 123 } 124 } 125 126 @Override 127 public boolean onPrepareOptionsMenu(Menu menu) { 128 super.onPrepareOptionsMenu(menu); 129 if (mTask == null) { 130 return false; 131 } else { 132 return mTask.onPrepareOptionsMenu(menu); 133 } 134 } 135 136 @Override 137 public void onActivityResult(int requestCode, int resultCode, Intent data) { 138 if (mTask != null) { 139 mTask.onActivityResult(requestCode, resultCode, data); 140 } 141 } 142 143 @Override 144 public boolean onKeyDown(int keyCode, KeyEvent event) { 145 if (mTask != null) { 146 return mTask.onKeyDown(keyCode, event); 147 } 148 return false; 149 } 150} 151