1b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate/*
2b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * Copyright (C) 2006 The Android Open Source Project
3b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate *
4b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * Licensed under the Apache License, Version 2.0 (the "License");
5b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * you may not use this file except in compliance with the License.
6b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * You may obtain a copy of the License at
7b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate *
8b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate *      http://www.apache.org/licenses/LICENSE-2.0
9b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate *
10b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * Unless required by applicable law or agreed to in writing, software
11b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * distributed under the License is distributed on an "AS IS" BASIS,
12b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * See the License for the specific language governing permissions and
14b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * limitations under the License.
15b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate */
16b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
17b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tatepackage com.android.largeassettest;
18b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
19b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport android.app.Activity;
20b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport android.content.Context;
21b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport android.content.res.AssetManager;
22b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport android.os.AsyncTask;
23b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport android.os.Bundle;
24b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport android.util.Log;
25b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport android.view.View;
26b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport android.widget.Button;
27b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport android.widget.TextView;
28b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
29b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport java.io.InputStream;
30b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tateimport java.io.IOException;
31b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
32b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate/**
33b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * Skeleton to test large-asset handling.  The asset in question is one million
34b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate * four-byte integers, in ascending numeric order.
35b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate */
36b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tatepublic class LargeAssetTest extends Activity {
37b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    Button mValidateButton;
38b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    TextView mResultText;
39b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    Validator mValidateThread;
40b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
41b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    @Override
42b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    protected void onCreate(Bundle icicle) {
43b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        super.onCreate(icicle);
44b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        setContentView(R.layout.lat);
45b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
46b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        mResultText = (TextView) findViewById(R.id.result);
47b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        mValidateButton = (Button) findViewById(R.id.validate);
48b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
49b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        mValidateButton.setOnClickListener(mClickListener);
50b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    }
51b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
52b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    View.OnClickListener mClickListener = new View.OnClickListener() {
53b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        public void onClick(View v) {
54b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            mValidateButton.setEnabled(false);
55b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            mValidateThread = new Validator();
56b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            mValidateThread.execute(LargeAssetTest.this.getAssets());
57b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        }
58b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    };
59b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
60b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    /**
61b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate     * Validation happens in a separate thread
62b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate     */
63b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    class Validator extends AsyncTask<AssetManager, Integer, Boolean> {
64b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        static final String TAG = "Validator";
65b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
66b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        @Override
67b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        protected Boolean doInBackground(AssetManager... params) {
68b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            AssetManager am = params[0];
69b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            try {
70b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                InputStream is = am.open("million-ints", AssetManager.ACCESS_STREAMING);
71b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                byte[] buf = new byte[4];
72b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
73b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                for (int i = 0; i < 1000000; i++) {
74b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                    int num = is.read(buf, 0, 4);
75b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                    if (num != 4) {
76b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                        Log.e(TAG, "Wanted 4 bytes but read " + num);
77b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                        return false;
78b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                    }
79b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                    // the byte array is stored in the asset in little-endian order
80b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                    int value = (buf[3] << 24) + ((buf[2] & 0xFF) << 16)
81b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                            + ((buf[1] & 0xFF) << 8) + (buf[0] & 0xFF);
82b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                    if (value != i) {
83b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                        Log.e(TAG, "Mismatch: index " + i + " : value " + value);
84b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                        return false;
85b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                    }
86b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                }
87b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
88b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                is.close();
89b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            } catch (IOException e) {
90b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                Log.w(TAG, "Couldn't open asset", e);
91b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate                return false;
92b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            }
93b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            Log.i(TAG, "Finished, reporting valid");
94b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            return true;
95b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        }
96b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate
97b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        @Override
98b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        protected void onPostExecute(Boolean result) {
99b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            CharSequence text = (result) ? "Valid!" : "NOT VALID";
100b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            mResultText.setText(text);
101b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate            mValidateButton.setEnabled(true);
102b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate        }
103b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate    }
104b100cbf178e91d6652ebbad3ed36684cacb9d10eChristopher Tate}
105