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