use of android.os.Message in project android_frameworks_base by ParanoidAndroid.
the class LoaderTestCase method getLoaderResultSynchronously.
/**
* Runs a Loader synchronously and returns the result of the load. The loader will
* be started, stopped, and destroyed by this method so it cannot be reused.
*
* @param loader The loader to run synchronously
* @return The result from the loader
*/
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
// The test thread blocks on this queue until the loader puts it's result in
final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);
// This callback runs on the "main" thread and unblocks the test thread
// when it puts the result into the blocking queue
final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() {
@Override
public void onLoadComplete(Loader<T> completedLoader, T data) {
// Shut the loader down
completedLoader.unregisterListener(this);
completedLoader.stopLoading();
completedLoader.reset();
// Store the result, unblocking the test thread
queue.add(data);
}
};
// This handler runs on the "main" thread of the process since AsyncTask
// is documented as needing to run on the main thread and many Loaders use
// AsyncTask
final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
loader.registerListener(0, listener);
loader.startLoading();
}
};
// Ask the main thread to start the loading process
mainThreadHandler.sendEmptyMessage(0);
// Block on the queue waiting for the result of the load to be inserted
T result;
while (true) {
try {
result = queue.take();
break;
} catch (InterruptedException e) {
throw new RuntimeException("waiting thread interrupted", e);
}
}
return result;
}
use of android.os.Message in project android_frameworks_base by ParanoidAndroid.
the class PowerMeasurement method testPageLoadStaticNYTimes.
public void testPageLoadStaticNYTimes() throws Throwable {
Instrumentation mInst = getInstrumentation();
PowerTestActivity act = getActivity();
Intent intent = new Intent(mInst.getContext(), PowerTestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
long start = System.currentTimeMillis();
PowerTestActivity activity = (PowerTestActivity) mInst.startActivitySync(intent);
activity.reset();
//send a message with the new URL
Handler handler = activity.getHandler();
Message msg = handler.obtainMessage(PowerTestActivity.MSG_NAVIGATE, TIME_OUT, DELAY);
msg.getData().putString(PowerTestActivity.MSG_NAV_URL, TESTING_URL);
msg.getData().putBoolean(PowerTestActivity.MSG_NAV_LOGTIME, true);
handler.sendMessage(msg);
boolean timeoutFlag = activity.waitUntilDone();
long end = System.currentTimeMillis();
assertFalse(TESTING_URL + " failed to load", timeoutFlag);
boolean pageErrorFlag = activity.getPageError();
assertFalse(TESTING_URL + " is not available, either network is down or the server is down", pageErrorFlag);
Log.v(LOGTAG, "Page is loaded in " + activity.getPageLoadTime() + " ms.");
// Force to clean up the cache dir so that it get back to the clean
// state
Runtime fileRemoval = Runtime.getRuntime();
String cmdBecomeSu = "su";
boolean clearCacheSuccess = false;
try {
Process runsum = fileRemoval.exec(cmdBecomeSu);
int exitVal = runsum.waitFor();
String rmfile = "rm -r /data/data/com.android.browserpowertest/cache";
Process removal = fileRemoval.exec(rmfile);
exitVal = removal.waitFor();
if (exitVal == 0) {
clearCacheSuccess = true;
}
} catch (Exception e) {
assertTrue("Fails to clear the cahche", false);
}
assertTrue("Fails to clear the cahche", clearCacheSuccess);
activity.finish();
}
use of android.os.Message in project android_frameworks_base by ParanoidAndroid.
the class PowerTestActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(LOGTAG, "onCreate, inst=" + Integer.toHexString(hashCode()));
LinearLayout contentView = new LinearLayout(this);
contentView.setOrientation(LinearLayout.VERTICAL);
setContentView(contentView);
setTitle("Idle");
webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
webViewClient = new SimpleWebViewClient();
chromeClient = new SimpleChromeClient();
webView.setWebViewClient(webViewClient);
webView.setWebChromeClient(chromeClient);
contentView.addView(webView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0.0f));
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case MSG_TIMEOUT:
handleTimeout();
return;
case MSG_NAVIGATE:
manualDelay = msg.arg2;
navigate(msg.getData().getString(MSG_NAV_URL), msg.arg1);
logTime = msg.getData().getBoolean(MSG_NAV_LOGTIME);
return;
}
}
};
pageDoneLock = new Object();
}
use of android.os.Message in project android_frameworks_base by ParanoidAndroid.
the class ReliabilityTestActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(LOGTAG, "onCreate, inst=" + Integer.toHexString(hashCode()));
LinearLayout contentView = new LinearLayout(this);
contentView.setOrientation(LinearLayout.VERTICAL);
setContentView(contentView);
setTitle("Idle");
webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
webViewClient = new SimpleWebViewClient();
chromeClient = new SimpleChromeClient();
webView.setWebViewClient(webViewClient);
webView.setWebChromeClient(chromeClient);
contentView.addView(webView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0.0f));
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case MSG_TIMEOUT:
handleTimeout();
return;
case MSG_NAVIGATE:
manualDelay = msg.arg2;
navigate(msg.getData().getString(MSG_NAV_URL), msg.arg1);
logTime = msg.getData().getBoolean(MSG_NAV_LOGTIME);
return;
}
}
};
pageDoneLock = new Object();
}
use of android.os.Message in project android_frameworks_base by ParanoidAndroid.
the class DirListActivity method showDir.
/**
* Loads the contents of dir into the list view.
*
* @param dirPath
* directory to load into list view
*/
private void showDir(String dirPath) {
mCurrentDirPath = dirPath;
/** Show progress dialog with a delay */
final Handler delayedDialogHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == MSG_SHOW_PROGRESS_DIALOG) {
if (sProgressDialog == null) {
sProgressDialog = new ProgressDialog(DirListActivity.this);
sProgressDialog.setCancelable(false);
sProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
sProgressDialog.setTitle(R.string.dialog_progress_title);
sProgressDialog.setMessage(getText(R.string.dialog_progress_msg));
}
sProgressDialog.show();
}
}
};
Message msgShowDialog = delayedDialogHandler.obtainMessage(MSG_SHOW_PROGRESS_DIALOG);
delayedDialogHandler.sendMessageDelayed(msgShowDialog, PROGRESS_DIALOG_DELAY_MS);
/** Delegate loading contents from SD card to a new thread */
new LoadListItemsThread(mCurrentDirPath, new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == MSG_LOADED_ITEMS) {
setTitle(shortenTitle(mCurrentDirPath));
delayedDialogHandler.removeMessages(MSG_SHOW_PROGRESS_DIALOG);
if (sProgressDialog != null) {
sProgressDialog.dismiss();
}
if (msg.obj == null) {
Toast.makeText(DirListActivity.this, NO_RESPONSE_MESSAGE, Toast.LENGTH_LONG).show();
} else {
setListAdapter(new DirListAdapter(DirListActivity.this, (ListItem[]) msg.obj));
}
}
}
}).start();
}
Aggregations