Home | History | Annotate | Download | only in largeassettest
      1 /*
      2  * Copyright (C) 2006 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 
     17 package com.android.largeassettest;
     18 
     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;
     28 
     29 import java.io.InputStream;
     30 import java.io.IOException;
     31 
     32 /**
     33  * Skeleton to test large-asset handling.  The asset in question is one million
     34  * four-byte integers, in ascending numeric order.
     35  */
     36 public class LargeAssetTest extends Activity {
     37     Button mValidateButton;
     38     TextView mResultText;
     39     Validator mValidateThread;
     40 
     41     @Override
     42     protected void onCreate(Bundle icicle) {
     43         super.onCreate(icicle);
     44         setContentView(R.layout.lat);
     45 
     46         mResultText = (TextView) findViewById(R.id.result);
     47         mValidateButton = (Button) findViewById(R.id.validate);
     48 
     49         mValidateButton.setOnClickListener(mClickListener);
     50     }
     51 
     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());
     57         }
     58     };
     59 
     60     /**
     61      * Validation happens in a separate thread
     62      */
     63     class Validator extends AsyncTask<AssetManager, Integer, Boolean> {
     64         static final String TAG = "Validator";
     65 
     66         @Override
     67         protected Boolean doInBackground(AssetManager... params) {
     68             AssetManager am = params[0];
     69             try {
     70                 InputStream is = am.open("million-ints", AssetManager.ACCESS_STREAMING);
     71                 byte[] buf = new byte[4];
     72 
     73                 for (int i = 0; i < 1000000; i++) {
     74                     int num = is.read(buf, 0, 4);
     75                     if (num != 4) {
     76                         Log.e(TAG, "Wanted 4 bytes but read " + num);
     77                         return false;
     78                     }
     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);
     82                     if (value != i) {
     83                         Log.e(TAG, "Mismatch: index " + i + " : value " + value);
     84                         return false;
     85                     }
     86                 }
     87 
     88                 is.close();
     89             } catch (IOException e) {
     90                 Log.w(TAG, "Couldn't open asset", e);
     91                 return false;
     92             }
     93             Log.i(TAG, "Finished, reporting valid");
     94             return true;
     95         }
     96 
     97         @Override
     98         protected void onPostExecute(Boolean result) {
     99             CharSequence text = (result) ? "Valid!" : "NOT VALID";
    100             mResultText.setText(text);
    101             mValidateButton.setEnabled(true);
    102         }
    103     }
    104 }
    105