blob: 7cce5439c8e2a167b1396447263a0d2f508c1419 [file] [log] [blame]
Kenny Rootb9e80e72010-08-09 17:25:00 -07001/*
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
17package com.example.android.obbapp;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.os.Environment;
22import android.os.storage.OnObbStateChangeListener;
23import android.os.storage.StorageManager;
24import android.util.Log;
25import android.view.View;
26import android.view.View.OnClickListener;
27import android.widget.Button;
28import android.widget.TextView;
29
30import 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 */
38public 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, String state) {
81 Log.d(TAG, "path=" + path + "; state=" + state);
82 mStatus.setText(state);
83 if (state.equals(Environment.MEDIA_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}