Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2010 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.android.gallery3d.app;
     18 
     19 import com.android.gallery3d.ui.GLView;
     20 
     21 import android.app.ActionBar;
     22 import android.app.Activity;
     23 import android.content.BroadcastReceiver;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.content.IntentFilter;
     27 import android.content.Intent;
     28 import android.content.res.Configuration;
     29 import android.os.BatteryManager;
     30 import android.os.Bundle;
     31 import android.view.Menu;
     32 import android.view.MenuItem;
     33 import android.view.View;
     34 import android.view.Window;
     35 import android.view.WindowManager;
     36 
     37 abstract public class ActivityState {
     38     public static final int FLAG_HIDE_ACTION_BAR = 1;
     39     public static final int FLAG_HIDE_STATUS_BAR = 2;
     40     public static final int FLAG_SCREEN_ON = 3;
     41 
     42     private static final int SCREEN_ON_FLAGS = (
     43               WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
     44             | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
     45             | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
     46         );
     47 
     48     protected GalleryActivity mActivity;
     49     protected Bundle mData;
     50     protected int mFlags;
     51 
     52     protected ResultEntry mReceivedResults;
     53     protected ResultEntry mResult;
     54 
     55     protected static class ResultEntry {
     56         public int requestCode;
     57         public int resultCode = Activity.RESULT_CANCELED;
     58         public Intent resultData;
     59         ResultEntry next;
     60     }
     61 
     62     private boolean mDestroyed = false;
     63     private boolean mPlugged = false;
     64 
     65     protected ActivityState() {
     66     }
     67 
     68     protected void setContentPane(GLView content) {
     69         mActivity.getGLRoot().setContentPane(content);
     70     }
     71 
     72     void initialize(GalleryActivity activity, Bundle data) {
     73         mActivity = activity;
     74         mData = data;
     75     }
     76 
     77     public Bundle getData() {
     78         return mData;
     79     }
     80 
     81     protected void onBackPressed() {
     82         mActivity.getStateManager().finishState(this);
     83     }
     84 
     85     protected void setStateResult(int resultCode, Intent data) {
     86         if (mResult == null) return;
     87         mResult.resultCode = resultCode;
     88         mResult.resultData = data;
     89     }
     90 
     91     protected void onConfigurationChanged(Configuration config) {
     92     }
     93 
     94     protected void onSaveState(Bundle outState) {
     95     }
     96 
     97     protected void onStateResult(int requestCode, int resultCode, Intent data) {
     98     }
     99 
    100     protected void onCreate(Bundle data, Bundle storedState) {
    101     }
    102 
    103     BroadcastReceiver mPowerIntentReceiver = new BroadcastReceiver() {
    104         @Override
    105         public void onReceive(Context context, Intent intent) {
    106             final String action = intent.getAction();
    107             if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
    108                 boolean plugged = (0 != intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0));
    109 
    110                 if (plugged != mPlugged) {
    111                     mPlugged = plugged;
    112                     final Window win = ((Activity) mActivity).getWindow();
    113                     final WindowManager.LayoutParams params = win.getAttributes();
    114                     setScreenOnFlags(params);
    115                     win.setAttributes(params);
    116                 }
    117             }
    118         }
    119     };
    120 
    121     void setScreenOnFlags(WindowManager.LayoutParams params) {
    122         if (mPlugged && 0 != (mFlags & FLAG_SCREEN_ON)) {
    123             params.flags |= SCREEN_ON_FLAGS;
    124         } else {
    125             params.flags &= ~SCREEN_ON_FLAGS;
    126         }
    127     }
    128 
    129     protected void onPause() {
    130         if (0 != (mFlags & FLAG_SCREEN_ON)) {
    131             ((Activity) mActivity).unregisterReceiver(mPowerIntentReceiver);
    132         }
    133     }
    134 
    135     // should only be called by StateManager
    136     void resume() {
    137         Activity activity = (Activity) mActivity;
    138         ActionBar actionBar = activity.getActionBar();
    139         if (actionBar != null) {
    140             if ((mFlags & FLAG_HIDE_ACTION_BAR) != 0) {
    141                 actionBar.hide();
    142             } else {
    143                 actionBar.show();
    144             }
    145             int stateCount = mActivity.getStateManager().getStateCount();
    146             actionBar.setDisplayOptions(
    147                     stateCount == 1 ? 0 : ActionBar.DISPLAY_HOME_AS_UP,
    148                     ActionBar.DISPLAY_HOME_AS_UP);
    149             actionBar.setHomeButtonEnabled(true);
    150         }
    151 
    152         activity.invalidateOptionsMenu();
    153 
    154         final Window win = activity.getWindow();
    155         final WindowManager.LayoutParams params = win.getAttributes();
    156 
    157         if ((mFlags & FLAG_HIDE_STATUS_BAR) != 0) {
    158             params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
    159         } else {
    160             params.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE;
    161         }
    162 
    163         setScreenOnFlags(params);
    164         win.setAttributes(params);
    165 
    166         ResultEntry entry = mReceivedResults;
    167         if (entry != null) {
    168             mReceivedResults = null;
    169             onStateResult(entry.requestCode, entry.resultCode, entry.resultData);
    170         }
    171 
    172         if (0 != (mFlags & FLAG_SCREEN_ON)) {
    173             // we need to know whether the device is plugged in to do this correctly
    174             final IntentFilter filter = new IntentFilter();
    175             filter.addAction(Intent.ACTION_BATTERY_CHANGED);
    176             activity.registerReceiver(mPowerIntentReceiver, filter);
    177         }
    178         onResume();
    179     }
    180 
    181     // a subclass of ActivityState should override the method to resume itself
    182     protected void onResume() {
    183     }
    184 
    185     protected boolean onCreateActionBar(Menu menu) {
    186         // TODO: we should return false if there is no menu to show
    187         //       this is a workaround for a bug in system
    188         return true;
    189     }
    190 
    191     protected boolean onItemSelected(MenuItem item) {
    192         return false;
    193     }
    194 
    195     protected void onDestroy() {
    196         mDestroyed = true;
    197     }
    198 
    199     boolean isDestroyed() {
    200         return mDestroyed;
    201     }
    202 }
    203