Wait until installed app service is ready before updating app details.

We were jumping the gun before, and asking to refresh the app details
adapter before the installed app service was able to notify us of a
change to the apps installed version.

This should be refactored to use broadcast receivers instead of
content observers (which are tied to the implementation of a content
provider). However this is currently a straight port from app details 1
to app details 2.
This commit is contained in:
Peter Serwylo 2017-04-05 16:27:37 +10:00
parent 50044629ab
commit 24a6e9590d

View File

@ -9,8 +9,10 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.content.LocalBroadcastManager;
@ -188,6 +190,13 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog
if (app != null) {
visiblePackageName = app.packageName;
}
appObserver = new AppObserver(new Handler());
getContentResolver().registerContentObserver(
AppProvider.getHighestPriorityMetadataUri(app.packageName),
true,
appObserver);
updateNotificationsForApp();
}
@ -195,6 +204,8 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog
super.onStop();
visiblePackageName = null;
getContentResolver().unregisterContentObserver(appObserver);
// When leaving the app details, make sure to refresh app status for this app, since
// we might want to show notifications for it now.
updateNotificationsForApp();
@ -432,7 +443,9 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog
case Installer.ACTION_INSTALL_COMPLETE:
adapter.clearProgress();
unregisterInstallReceiver();
onAppChanged();
// Don't try and update the view here, because the InstalledAppProviderService
// hasn't had time to do its thing and mark the app as installed. Instead, we
// wait for that service to notify us, and then we will respond in appObserver.
break;
case Installer.ACTION_INSTALL_INTERRUPTED:
adapter.clearProgress();
@ -655,4 +668,26 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog
}
startUninstall();
}
// observer to update view when package has been installed/deleted
private AppObserver appObserver;
class AppObserver extends ContentObserver {
AppObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
onChange(selfChange, null);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
onAppChanged();
}
}
}