• 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.View;
29 
30 import com.googlecode.android_scripting.BaseApplication;
31 import com.googlecode.android_scripting.Constants;
32 import com.googlecode.android_scripting.FutureActivityTaskExecutor;
33 import com.googlecode.android_scripting.Log;
34 import com.googlecode.android_scripting.future.FutureActivityTask;
35 import 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  */
43 public class FutureActivity extends Activity {
44   private FutureActivityTask<?> mTask;
45 
46   @Override
onCreate(Bundle savedInstanceState)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
onStart()80   protected void onStart() {
81     super.onStart();
82     if (mTask != null) {
83       mTask.onStart();
84     }
85   }
86 
87   @Override
onResume()88   protected void onResume() {
89     super.onResume();
90     if (mTask != null) {
91       mTask.onResume();
92     }
93   }
94 
95   @Override
onPause()96   protected void onPause() {
97     super.onPause();
98     if (mTask != null) {
99       mTask.onPause();
100     }
101   }
102 
103   @Override
onStop()104   protected void onStop() {
105     super.onStop();
106     if (mTask != null) {
107       mTask.onStop();
108     }
109   }
110 
111   @Override
onDestroy()112   protected void onDestroy() {
113     super.onDestroy();
114     if (mTask != null) {
115       mTask.onDestroy();
116     }
117   }
118 
119   @Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)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
onPrepareOptionsMenu(Menu menu)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
onActivityResult(int requestCode, int resultCode, Intent data)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
onKeyDown(int keyCode, KeyEvent event)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