summaryrefslogtreecommitdiff
path: root/samples/Obb/src/com/example/android/obbapp/ObbMountActivity.java
blob: c4e952c1bc0d0116731ee00f93cbe9d9cc4f85c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.obbapp;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.storage.OnObbStateChangeListener;
import android.os.storage.StorageManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import java.io.File;

/**
 * This class provides a basic demonstration of how to manage an OBB file. It
 * provides two buttons: one to mount an OBB and another to unmount an OBB. The
 * main feature is that it implements an OnObbStateChangeListener which updates
 * some text fields with relevant information.
 */
public class ObbMountActivity extends Activity {
    private static final String TAG = "ObbMount";

    private static String mObbPath;

    private TextView mStatus;
    private TextView mPath;

    private StorageManager mSM;

    /** Called with the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Inflate our UI from its XML layout description.
        setContentView(R.layout.obb_mount_activity);

        // Hook up button presses to the appropriate event handler.
        ((Button) findViewById(R.id.mount)).setOnClickListener(mMountListener);
        ((Button) findViewById(R.id.unmount)).setOnClickListener(mUnmountListener);

        // Text indications of current status
        mStatus = (TextView) findViewById(R.id.status);
        mPath = (TextView) findViewById(R.id.path);

        ObbState state = (ObbState) getLastNonConfigurationInstance();

        if (state != null) {
            mSM = state.storageManager;
            mStatus.setText(state.status);
            mPath.setText(state.path);
        } else {
            // Get an instance of the StorageManager
            mSM = (StorageManager) getApplicationContext().getSystemService(STORAGE_SERVICE);
        }

        mObbPath = new File(Environment.getExternalStorageDirectory(), "test1.obb").getPath();
    }

    OnObbStateChangeListener mEventListener = new OnObbStateChangeListener() {
        @Override
        public void onObbStateChange(String path, int state) {
            Log.d(TAG, "path=" + path + "; state=" + state);
            mStatus.setText(String.valueOf(state));
            if (state == OnObbStateChangeListener.MOUNTED) {
                mPath.setText(mSM.getMountedObbPath(mObbPath));
            } else {
                mPath.setText("");
            }
        }
    };

    /**
     * A call-back for when the user presses the back button.
     */
    OnClickListener mMountListener = new OnClickListener() {
        public void onClick(View v) {
            try {
                // We don't need to synchronize here to avoid clobbering the
                // content of mStatus because the callback comes to our main
                // looper.
                if (mSM.mountObb(mObbPath, null, mEventListener)) {
                    mStatus.setText(R.string.attempting_mount);
                } else {
                    mStatus.setText(R.string.failed_to_start_mount);
                }
            } catch (IllegalArgumentException e) {
                mStatus.setText(R.string.obb_already_mounted);
                Log.d(TAG, "OBB already mounted");
            }
        }
    };

    /**
     * A call-back for when the user presses the clear button.
     */
    OnClickListener mUnmountListener = new OnClickListener() {
        public void onClick(View v) {
            try {
                if (mSM.unmountObb(mObbPath, false, mEventListener)) {
                    mStatus.setText(R.string.attempting_unmount);
                } else {
                    mStatus.setText(R.string.failed_to_start_unmount);
                }
            } catch (IllegalArgumentException e) {
                mStatus.setText(R.string.obb_not_mounted);
                Log.d(TAG, "OBB not mounted");
            }
        }
    };

    @Override
    public Object onRetainNonConfigurationInstance() {
        // Since our OBB mount is tied to the StorageManager, retain it
        ObbState state = new ObbState(mSM, mStatus.getText(), mPath.getText());
        return state;
    }

    private static class ObbState {
        public StorageManager storageManager;
        public CharSequence status;
        public CharSequence path;

        ObbState(StorageManager storageManager, CharSequence status, CharSequence path) {
            this.storageManager = storageManager;
            this.status = status;
            this.path = path;
        }
    }
}