Home | History | Annotate | Download | only in obbapp
      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.example.android.obbapp;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 import android.os.Environment;
     22 import android.os.storage.OnObbStateChangeListener;
     23 import android.os.storage.StorageManager;
     24 import android.util.Log;
     25 import android.view.View;
     26 import android.view.View.OnClickListener;
     27 import android.widget.Button;
     28 import android.widget.TextView;
     29 
     30 import java.io.File;
     31 
     32 /**
     33  * This class provides a basic demonstration of how to manage an OBB file. It
     34  * provides two buttons: one to mount an OBB and another to unmount an OBB. The
     35  * main feature is that it implements an OnObbStateChangeListener which updates
     36  * some text fields with relevant information.
     37  */
     38 public class ObbMountActivity extends Activity {
     39     private static final String TAG = "ObbMount";
     40 
     41     private static String mObbPath;
     42 
     43     private TextView mStatus;
     44     private TextView mPath;
     45 
     46     private StorageManager mSM;
     47 
     48     /** Called with the activity is first created. */
     49     @Override
     50     public void onCreate(Bundle savedInstanceState) {
     51         super.onCreate(savedInstanceState);
     52 
     53         // Inflate our UI from its XML layout description.
     54         setContentView(R.layout.obb_mount_activity);
     55 
     56         // Hook up button presses to the appropriate event handler.
     57         ((Button) findViewById(R.id.mount)).setOnClickListener(mMountListener);
     58         ((Button) findViewById(R.id.unmount)).setOnClickListener(mUnmountListener);
     59 
     60         // Text indications of current status
     61         mStatus = (TextView) findViewById(R.id.status);
     62         mPath = (TextView) findViewById(R.id.path);
     63 
     64         ObbState state = (ObbState) getLastNonConfigurationInstance();
     65 
     66         if (state != null) {
     67             mSM = state.storageManager;
     68             mStatus.setText(state.status);
     69             mPath.setText(state.path);
     70         } else {
     71             // Get an instance of the StorageManager
     72             mSM = (StorageManager) getApplicationContext().getSystemService(STORAGE_SERVICE);
     73         }
     74 
     75         mObbPath = new File(Environment.getExternalStorageDirectory(), "test1.obb").getPath();
     76     }
     77 
     78     OnObbStateChangeListener mEventListener = new OnObbStateChangeListener() {
     79         @Override
     80         public void onObbStateChange(String path, int state) {
     81             Log.d(TAG, "path=" + path + "; state=" + state);
     82             mStatus.setText(String.valueOf(state));
     83             if (state == OnObbStateChangeListener.MOUNTED) {
     84                 mPath.setText(mSM.getMountedObbPath(mObbPath));
     85             } else {
     86                 mPath.setText("");
     87             }
     88         }
     89     };
     90 
     91     /**
     92      * A call-back for when the user presses the back button.
     93      */
     94     OnClickListener mMountListener = new OnClickListener() {
     95         public void onClick(View v) {
     96             try {
     97                 // We don't need to synchronize here to avoid clobbering the
     98                 // content of mStatus because the callback comes to our main
     99                 // looper.
    100                 if (mSM.mountObb(mObbPath, null, mEventListener)) {
    101                     mStatus.setText(R.string.attempting_mount);
    102                 } else {
    103                     mStatus.setText(R.string.failed_to_start_mount);
    104                 }
    105             } catch (IllegalArgumentException e) {
    106                 mStatus.setText(R.string.obb_already_mounted);
    107                 Log.d(TAG, "OBB already mounted");
    108             }
    109         }
    110     };
    111 
    112     /**
    113      * A call-back for when the user presses the clear button.
    114      */
    115     OnClickListener mUnmountListener = new OnClickListener() {
    116         public void onClick(View v) {
    117             try {
    118                 if (mSM.unmountObb(mObbPath, false, mEventListener)) {
    119                     mStatus.setText(R.string.attempting_unmount);
    120                 } else {
    121                     mStatus.setText(R.string.failed_to_start_unmount);
    122                 }
    123             } catch (IllegalArgumentException e) {
    124                 mStatus.setText(R.string.obb_not_mounted);
    125                 Log.d(TAG, "OBB not mounted");
    126             }
    127         }
    128     };
    129 
    130     @Override
    131     public Object onRetainNonConfigurationInstance() {
    132         // Since our OBB mount is tied to the StorageManager, retain it
    133         ObbState state = new ObbState(mSM, mStatus.getText(), mPath.getText());
    134         return state;
    135     }
    136 
    137     private static class ObbState {
    138         public StorageManager storageManager;
    139         public CharSequence status;
    140         public CharSequence path;
    141 
    142         ObbState(StorageManager storageManager, CharSequence status, CharSequence path) {
    143             this.storageManager = storageManager;
    144             this.status = status;
    145             this.path = path;
    146         }
    147     }
    148 }
    149