Use ContentObserver to observe install status
This commit is contained in:
parent
919f9c63b8
commit
04577d213c
@ -33,18 +33,17 @@ import android.app.ListActivity;
|
|||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.nfc.NfcAdapter;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v4.app.NavUtils;
|
import android.support.v4.app.NavUtils;
|
||||||
import android.support.v4.view.MenuItemCompat;
|
import android.support.v4.view.MenuItemCompat;
|
||||||
import android.content.pm.ApplicationInfo;
|
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.Signature;
|
import android.content.pm.Signature;
|
||||||
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
|
import android.database.ContentObserver;
|
||||||
import android.text.Editable;
|
import android.text.Editable;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
import android.text.Html.TagHandler;
|
import android.text.Html.TagHandler;
|
||||||
@ -96,6 +95,31 @@ public class AppDetails extends ListActivity {
|
|||||||
TextView added;
|
TextView added;
|
||||||
TextView nativecode;
|
TextView nativecode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// observer to update view when package has been installed/removed
|
||||||
|
AppObserver myAppObserver;
|
||||||
|
class AppObserver extends ContentObserver {
|
||||||
|
public AppObserver(Handler handler) {
|
||||||
|
super(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChange(boolean selfChange) {
|
||||||
|
this.onChange(selfChange, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChange(boolean selfChange, Uri uri) {
|
||||||
|
if (!reset()) {
|
||||||
|
AppDetails.this.finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
updateViews();
|
||||||
|
|
||||||
|
MenuManager.create(AppDetails.this).invalidateOptionsMenu();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private class ApkListAdapter extends ArrayAdapter<Apk> {
|
private class ApkListAdapter extends ArrayAdapter<Apk> {
|
||||||
|
|
||||||
@ -323,7 +347,6 @@ public class AppDetails extends ListActivity {
|
|||||||
finish();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
resetRequired = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedPreferences prefs = PreferenceManager
|
SharedPreferences prefs = PreferenceManager
|
||||||
@ -347,7 +370,6 @@ public class AppDetails extends ListActivity {
|
|||||||
private boolean pref_expert;
|
private boolean pref_expert;
|
||||||
private boolean pref_permissions;
|
private boolean pref_permissions;
|
||||||
private boolean pref_incompatibleVersions;
|
private boolean pref_incompatibleVersions;
|
||||||
private boolean resetRequired;
|
|
||||||
|
|
||||||
// The signature of the installed version.
|
// The signature of the installed version.
|
||||||
private Signature mInstalledSignature;
|
private Signature mInstalledSignature;
|
||||||
@ -357,12 +379,17 @@ public class AppDetails extends ListActivity {
|
|||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
Log.d(TAG, "onresume");
|
Log.d(TAG, "onresume");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
if (resetRequired) {
|
|
||||||
if (!reset()) {
|
// register observer to know when install status changes
|
||||||
finish();
|
myAppObserver = new AppObserver(new Handler());
|
||||||
return;
|
getContentResolver().registerContentObserver(
|
||||||
}
|
AppProvider.getContentUri(app.id),
|
||||||
resetRequired = false;
|
true,
|
||||||
|
myAppObserver);
|
||||||
|
|
||||||
|
if (!reset()) {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
updateViews();
|
updateViews();
|
||||||
|
|
||||||
@ -375,6 +402,9 @@ public class AppDetails extends ListActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
|
if (myAppObserver != null) {
|
||||||
|
getContentResolver().unregisterContentObserver(myAppObserver);
|
||||||
|
}
|
||||||
if (downloadHandler != null) {
|
if (downloadHandler != null) {
|
||||||
downloadHandler.stopUpdates();
|
downloadHandler.stopUpdates();
|
||||||
}
|
}
|
||||||
@ -964,51 +994,36 @@ public class AppDetails extends ListActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(final int operation) {
|
public void onSuccess(final int operation) {
|
||||||
// TODO: this is a hack!!!
|
runOnUiThread(new Runnable() {
|
||||||
// Currently the views are not automatically updated when the receivers are notified
|
|
||||||
// if an app is installed/removed
|
|
||||||
// We are currently waiting that the receivers change the database and then reload the view
|
|
||||||
//
|
|
||||||
// Better approach:
|
|
||||||
// Implement Android Loader that restarts automatically on db change!
|
|
||||||
Thread wait = new Thread(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
Log.d(TAG, "handling installer onSuccess");
|
||||||
Thread.sleep(2000);
|
|
||||||
} catch (InterruptedException e) {
|
notifyAppChanged(app.id);
|
||||||
|
|
||||||
|
if (operation == Installer.InstallerCallback.OPERATION_INSTALL) {
|
||||||
|
if (downloadHandler != null) {
|
||||||
|
downloadHandler = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
PackageManagerCompat.setInstaller(mPm, app.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
runOnUiThread(new Runnable() {
|
setProgressBarIndeterminateVisibility(false);
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Log.d(TAG, "handling installer onSuccess");
|
|
||||||
|
|
||||||
notifyAppChanged(app.id);
|
|
||||||
|
|
||||||
resetRequired = true;
|
|
||||||
|
|
||||||
if (operation == Installer.InstallerCallback.OPERATION_INSTALL) {
|
|
||||||
if (downloadHandler != null) {
|
|
||||||
downloadHandler = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
PackageManagerCompat.setInstaller(mPm, app.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: whole onResume?
|
|
||||||
onResume();
|
|
||||||
setProgressBarIndeterminateVisibility(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
wait.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(int operation, final int errorCode) {
|
public void onError(int operation, final int errorCode) {
|
||||||
if (errorCode != InstallerCallback.ERROR_CODE_CANCELED) {
|
if (errorCode == InstallerCallback.ERROR_CODE_CANCELED) {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
setProgressBarIndeterminateVisibility(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user