Add OBB usage example
Change-Id: I7da5ac0009cb32829e7c6470c0a60ebff2988905
This commit is contained in:
16
samples/Obb/Android.mk
Normal file
16
samples/Obb/Android.mk
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
LOCAL_PATH:= $(call my-dir)
|
||||||
|
include $(CLEAR_VARS)
|
||||||
|
|
||||||
|
LOCAL_MODULE_TAGS := samples
|
||||||
|
|
||||||
|
# Only compile source java files in this apk.
|
||||||
|
LOCAL_SRC_FILES := $(call all-java-files-under, src)
|
||||||
|
|
||||||
|
LOCAL_PACKAGE_NAME := ObbApp
|
||||||
|
|
||||||
|
LOCAL_SDK_VERSION := current
|
||||||
|
|
||||||
|
include $(BUILD_PACKAGE)
|
||||||
|
|
||||||
|
# Use the following include to make our test apk.
|
||||||
|
include $(call all-makefiles-under,$(LOCAL_PATH))
|
||||||
31
samples/Obb/AndroidManifest.xml
Normal file
31
samples/Obb/AndroidManifest.xml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2007 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="com.example.android.obbapp">
|
||||||
|
|
||||||
|
<!-- OBBs were introduced in SDK version 9 -->
|
||||||
|
<!-- <uses-sdk android:minSdkVersion="9"/> -->
|
||||||
|
|
||||||
|
<application android:label="@string/obb_app">
|
||||||
|
<activity android:name="ObbMountActivity">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
</application>
|
||||||
|
</manifest>
|
||||||
52
samples/Obb/res/layout/obb_mount_activity.xml
Normal file
52
samples/Obb/res/layout/obb_mount_activity.xml
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2007 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!-- This file describes the layout of the main SkeletonApp activity
|
||||||
|
user interface.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!-- The top view is a layout manager that places its child views into
|
||||||
|
a row, here set to be vertical (so the first is at the top) -->
|
||||||
|
|
||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||||
|
|
||||||
|
<Button android:id="@+id/mount"
|
||||||
|
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||||
|
android:text="@string/mount" />
|
||||||
|
|
||||||
|
<Button android:id="@+id/unmount"
|
||||||
|
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||||
|
android:layout_toRightOf="@+id/mount"
|
||||||
|
android:text="@string/unmount" />
|
||||||
|
|
||||||
|
<TextView android:id="@+id/status_label" android:layout_below="@+id/mount"
|
||||||
|
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||||
|
android:text="@string/status_label" />
|
||||||
|
|
||||||
|
<TextView android:id="@+id/status" android:layout_below="@+id/mount"
|
||||||
|
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||||
|
android:layout_toRightOf="@+id/status_label" />
|
||||||
|
|
||||||
|
<TextView android:id="@+id/path_label" android:layout_below="@+id/status_label"
|
||||||
|
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||||
|
android:text="@string/path_label" />
|
||||||
|
|
||||||
|
<TextView android:id="@+id/path" android:layout_below="@+id/status_label"
|
||||||
|
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||||
|
android:layout_toRightOf="@+id/path_label" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
37
samples/Obb/res/values/strings.xml
Normal file
37
samples/Obb/res/values/strings.xml
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!-- This file contains resource definitions for displayed strings, allowing
|
||||||
|
them to be changed based on the locale and options. -->
|
||||||
|
|
||||||
|
<resources>
|
||||||
|
<string name="obb_app">Obb App</string>
|
||||||
|
|
||||||
|
<string name="mount">Mount</string>
|
||||||
|
<string name="unmount">Unmount</string>
|
||||||
|
|
||||||
|
<string name="path_label">Path:</string>
|
||||||
|
<string name="status_label">Status:</string>
|
||||||
|
|
||||||
|
<string name="obb_already_mounted">OBB already mounted!</string>
|
||||||
|
<string name="obb_not_mounted">OBB is not mounted!</string>
|
||||||
|
|
||||||
|
<string name="failed_to_start_mount">Failed to start mount process...</string>
|
||||||
|
<string name="failed_to_start_unmount">Failed to start unmount process...</string>
|
||||||
|
|
||||||
|
<string name="attempting_mount">Attempting to mount...</string>
|
||||||
|
<string name="attempting_unmount">Attempting to unmount...</string>
|
||||||
|
</resources>
|
||||||
148
samples/Obb/src/com/example/android/obbapp/ObbMountActivity.java
Normal file
148
samples/Obb/src/com/example/android/obbapp/ObbMountActivity.java
Normal file
@@ -0,0 +1,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, String state) {
|
||||||
|
Log.d(TAG, "path=" + path + "; state=" + state);
|
||||||
|
mStatus.setText(state);
|
||||||
|
if (state.equals(Environment.MEDIA_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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user