| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 34 |
|
| 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 = findViewById(R.id.result); |
| 47 |
mValidateButton = 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 |
|
| 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 |
|
| 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 |
} |