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