Home | History | Annotate | Download | only in future
      1 /*
      2  * Copyright (C) 2016 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * 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  * @author Damon Kohler (damonkohler (at) gmail.com)
     33  */
     34 public abstract class FutureActivityTask<T> {
     35 
     36   private final FutureResult<T> mResult = new FutureResult<T>();
     37   private Activity mActivity;
     38 
     39   public void setActivity(Activity activity) {
     40     mActivity = activity;
     41   }
     42 
     43   public Activity getActivity() {
     44     return mActivity;
     45   }
     46 
     47   public void onCreate() {
     48     mActivity.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
     49   }
     50 
     51   public void onStart() {
     52   }
     53 
     54   public void onResume() {
     55   }
     56 
     57   public void onPause() {
     58   }
     59 
     60   public void onStop() {
     61   }
     62 
     63   public void onDestroy() {
     64   }
     65 
     66   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
     67   }
     68 
     69   public boolean onPrepareOptionsMenu(Menu menu) {
     70     return false;
     71   }
     72 
     73   public void onActivityResult(int requestCode, int resultCode, Intent data) {
     74   }
     75 
     76   protected void setResult(T result) {
     77     mResult.set(result);
     78   }
     79 
     80   public T getResult() throws InterruptedException {
     81     return mResult.get();
     82   }
     83 
     84   public T getResult(long timeout, TimeUnit unit) throws InterruptedException {
     85     return mResult.get(timeout, unit);
     86   }
     87 
     88   public void finish() {
     89     mActivity.finish();
     90   }
     91 
     92   public void startActivity(Intent intent) {
     93     mActivity.startActivity(intent);
     94   }
     95 
     96   public void startActivityForResult(Intent intent, int requestCode) {
     97     mActivity.startActivityForResult(intent, requestCode);
     98   }
     99 
    100   public boolean onKeyDown(int keyCode, KeyEvent event) {
    101     // Placeholder.
    102     return false;
    103   }
    104 }
    105