2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.largeassettest;
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.res.AssetManager;
22 import android.os.AsyncTask;
23 import android.os.Bundle;
24 import android.util.Log;
25 import android.view.View;
26 import android.widget.Button;
27 import android.widget.TextView;
29 import java.io.InputStream;
30 import java.io.IOException;
33 * Skeleton to test large-asset handling. The asset in question is one million
34 * four-byte integers, in ascending numeric order.
36 public class LargeAssetTest extends Activity {
37 Button mValidateButton;
39 Validator mValidateThread;
42 protected void onCreate(Bundle icicle) {
43 super.onCreate(icicle);
44 setContentView(R.layout.lat);
46 mResultText = (TextView) findViewById(R.id.result);
47 mValidateButton = (Button) findViewById(R.id.validate);
49 mValidateButton.setOnClickListener(mClickListener);
52 View.OnClickListener mClickListener = new View.OnClickListener() {
53 public void onClick(View v) {
54 mValidateButton.setEnabled(false);
55 mValidateThread = new Validator();
56 mValidateThread.execute(LargeAssetTest.this.getAssets());
61 * Validation happens in a separate thread
63 class Validator extends AsyncTask<AssetManager, Integer, Boolean> {
64 static final String TAG = "Validator";
67 protected Boolean doInBackground(AssetManager... params) {
68 AssetManager am = params[0];
70 InputStream is = am.open("million-ints", AssetManager.ACCESS_STREAMING);
71 byte[] buf = new byte[4];
73 for (int i = 0; i < 1000000; i++) {
74 int num = is.read(buf, 0, 4);
76 Log.e(TAG, "Wanted 4 bytes but read " + num);
79 // the byte array is stored in the asset in little-endian order
80 int value = (buf[3] << 24) + ((buf[2] & 0xFF) << 16)
81 + ((buf[1] & 0xFF) << 8) + (buf[0] & 0xFF);
83 Log.e(TAG, "Mismatch: index " + i + " : value " + value);
89 } catch (IOException e) {
90 Log.w(TAG, "Couldn't open asset", e);
93 Log.i(TAG, "Finished, reporting valid");
98 protected void onPostExecute(Boolean result) {
99 CharSequence text = (result) ? "Valid!" : "NOT VALID";
100 mResultText.setText(text);
101 mValidateButton.setEnabled(true);