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.future;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.view.ContextMenu;
22 import android.view.ContextMenu.ContextMenuInfo;
23 import android.view.KeyEvent;
24 import android.view.Menu;
25 import android.view.MotionEvent;
26 import android.view.View;
27 import android.view.Window;
28 import java.util.concurrent.TimeUnit;
29 
30 /**
31  * Encapsulates an {@link Activity} and a {@link FutureObject}.
32  *
33  */
34 public abstract class FutureActivityTask<T> {
35 
36   private final FutureResult<T> mResult = new FutureResult<T>();
37   private Activity mActivity;
38 
setActivity(Activity activity)39   public void setActivity(Activity activity) {
40     mActivity = activity;
41   }
42 
getActivity()43   public Activity getActivity() {
44     return mActivity;
45   }
46 
onCreate()47   public void onCreate() {
48     mActivity.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
49   }
50 
onStart()51   public void onStart() {
52   }
53 
onResume()54   public void onResume() {
55   }
56 
onPause()57   public void onPause() {
58   }
59 
onStop()60   public void onStop() {
61   }
62 
onDestroy()63   public void onDestroy() {
64   }
65 
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)66   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
67   }
68 
onPrepareOptionsMenu(Menu menu)69   public boolean onPrepareOptionsMenu(Menu menu) {
70     return false;
71   }
72 
onActivityResult(int requestCode, int resultCode, Intent data)73   public void onActivityResult(int requestCode, int resultCode, Intent data) {
74   }
75 
setResult(T result)76   protected void setResult(T result) {
77     mResult.set(result);
78   }
79 
getResult()80   public T getResult() throws InterruptedException {
81     return mResult.get();
82   }
83 
getResult(long timeout, TimeUnit unit)84   public T getResult(long timeout, TimeUnit unit) throws InterruptedException {
85     return mResult.get(timeout, unit);
86   }
87 
finish()88   public void finish() {
89     mActivity.finish();
90   }
91 
startActivity(Intent intent)92   public void startActivity(Intent intent) {
93     mActivity.startActivity(intent);
94   }
95 
startActivityForResult(Intent intent, int requestCode)96   public void startActivityForResult(Intent intent, int requestCode) {
97     mActivity.startActivityForResult(intent, requestCode);
98   }
99 
onKeyDown(int keyCode, KeyEvent event)100   public boolean onKeyDown(int keyCode, KeyEvent event) {
101     // Placeholder.
102     return false;
103   }
104 
onKeyUp(int keyCode, KeyEvent event)105   public boolean onKeyUp(int keyCode, KeyEvent event) {
106     // Placeholder.
107     return false;
108   }
109 
onGenericMotionEvent(MotionEvent event)110   public boolean onGenericMotionEvent(MotionEvent event) {
111     // Placeholder.
112     return false;
113   }
114 }
115