7777import static com .microsoft .azure .mobile .distribute .DistributeConstants .PREFERENCE_KEY_REQUEST_ID ;
7878import static com .microsoft .azure .mobile .distribute .DistributeConstants .PREFERENCE_KEY_UPDATE_TOKEN ;
7979import static com .microsoft .azure .mobile .distribute .DistributeConstants .SERVICE_NAME ;
80+ import static com .microsoft .azure .mobile .distribute .DistributeUtils .computeReleaseHash ;
8081import static com .microsoft .azure .mobile .distribute .DistributeUtils .getStoredDownloadState ;
8182import static com .microsoft .azure .mobile .http .DefaultHttpClient .METHOD_GET ;
8283
@@ -112,6 +113,11 @@ public class Distribute extends AbstractMobileCenterService {
112113 */
113114 private String mAppSecret ;
114115
116+ /**
117+ * Package info.
118+ */
119+ private PackageInfo mPackageInfo ;
120+
115121 /**
116122 * If not null we are in foreground inside this activity.
117123 */
@@ -278,6 +284,11 @@ public synchronized void onStarted(@NonNull Context context, @NonNull String app
278284 super .onStarted (context , appSecret , channel );
279285 mContext = context ;
280286 mAppSecret = appSecret ;
287+ try {
288+ mPackageInfo = mContext .getPackageManager ().getPackageInfo (mContext .getPackageName (), 0 );
289+ } catch (PackageManager .NameNotFoundException e ) {
290+ MobileCenterLog .error (LOG_TAG , "Could not get self package info." , e );
291+ }
281292 resumeDistributeWorkflow ();
282293 }
283294
@@ -398,7 +409,7 @@ private synchronized void cancelPreviousTasks() {
398409 * Method that triggers the distribute workflow or proceed to the next step.
399410 */
400411 private synchronized void resumeDistributeWorkflow () {
401- if (mForegroundActivity != null && !mWorkflowCompleted && isInstanceEnabled ()) {
412+ if (mPackageInfo != null && mForegroundActivity != null && !mWorkflowCompleted && isInstanceEnabled ()) {
402413
403414 /* Don't go any further it this is a debug app. */
404415 if ((mContext .getApplicationInfo ().flags & FLAG_DEBUGGABLE ) == FLAG_DEBUGGABLE ) {
@@ -440,15 +451,7 @@ private synchronized void resumeDistributeWorkflow() {
440451 if (downloadState != DOWNLOAD_STATE_COMPLETED && downloadState != DOWNLOAD_STATE_AVAILABLE && !mCheckedDownload ) {
441452
442453 /* Discard release if application updated. Then immediately check release. */
443- long lastUpdateTime ;
444- try {
445- lastUpdateTime = mContext .getPackageManager ().getPackageInfo (mContext .getPackageName (), 0 ).lastUpdateTime ;
446- } catch (PackageManager .NameNotFoundException e ) {
447- MobileCenterLog .debug (LOG_TAG , "Could not check last update time." , e );
448- completeWorkflow ();
449- return ;
450- }
451- if (lastUpdateTime > StorageHelper .PreferencesStorage .getLong (PREFERENCE_KEY_DOWNLOAD_TIME )) {
454+ if (mPackageInfo .lastUpdateTime > StorageHelper .PreferencesStorage .getLong (PREFERENCE_KEY_DOWNLOAD_TIME )) {
452455 MobileCenterLog .debug (LOG_TAG , "Discarding previous download as application updated." );
453456 cancelPreviousTasks ();
454457 }
@@ -546,7 +549,7 @@ else if (mUnknownSourcesDialog != null) {
546549
547550 /* If not, open browser to update setup. */
548551 if (!mBrowserOpenedOrAborted ) {
549- DistributeUtils .updateSetupUsingBrowser (mForegroundActivity , mInstallUrl , mAppSecret );
552+ DistributeUtils .updateSetupUsingBrowser (mForegroundActivity , mInstallUrl , mAppSecret , mPackageInfo );
550553 mBrowserOpenedOrAborted = true ;
551554 }
552555 }
@@ -623,7 +626,7 @@ synchronized void getLatestReleaseDetails(@NonNull String updateToken) {
623626 HttpClientRetryer retryer = new HttpClientRetryer (new DefaultHttpClient ());
624627 NetworkStateHelper networkStateHelper = NetworkStateHelper .getSharedInstance (mContext );
625628 HttpClient httpClient = new HttpClientNetworkStateHandler (retryer , networkStateHelper );
626- String url = mApiUrl + String .format (GET_LATEST_RELEASE_PATH_FORMAT , mAppSecret );
629+ String url = mApiUrl + String .format (GET_LATEST_RELEASE_PATH_FORMAT , mAppSecret , computeReleaseHash ( mPackageInfo ) );
627630 Map <String , String > headers = new HashMap <>();
628631 headers .put (HEADER_API_TOKEN , updateToken );
629632 final Object releaseCallId = mCheckReleaseCallId = new Object ();
@@ -738,38 +741,32 @@ else if (Build.VERSION.SDK_INT >= releaseDetails.getMinApiLevel()) {
738741
739742 /* Check version code is equals or higher and hash is different. */
740743 MobileCenterLog .debug (LOG_TAG , "Check if latest release is more recent." );
741- PackageManager packageManager = mContext .getPackageManager ();
742- try {
743- PackageInfo packageInfo = packageManager .getPackageInfo (mContext .getPackageName (), 0 );
744- if (isMoreRecent (packageInfo , releaseDetails )) {
745-
746- /* Update cache. */
747- PreferencesStorage .putString (PREFERENCE_KEY_RELEASE_DETAILS , rawReleaseDetails );
748-
749- /* If previous release is mandatory and still processing, don't do anything right now. */
750- if (mReleaseDetails != null && mReleaseDetails .isMandatoryUpdate ()) {
751- if (mReleaseDetails .getId () != releaseDetails .getId ()) {
752- MobileCenterLog .debug (LOG_TAG , "Latest release is more recent than the previous mandatory." );
753- PreferencesStorage .putInt (PREFERENCE_KEY_DOWNLOAD_STATE , DOWNLOAD_STATE_AVAILABLE );
754- } else {
755- MobileCenterLog .debug (LOG_TAG , "The latest release is mandatory and already being processed." );
756- }
757- return ;
758- }
759-
760- /* Show update dialog. */
761- mReleaseDetails = releaseDetails ;
762- MobileCenterLog .debug (LOG_TAG , "Latest release is more recent." );
763- PreferencesStorage .putInt (PREFERENCE_KEY_DOWNLOAD_STATE , DOWNLOAD_STATE_AVAILABLE );
764- if (mForegroundActivity != null ) {
765- showUpdateDialog ();
744+ if (isMoreRecent (releaseDetails )) {
745+
746+ /* Update cache. */
747+ PreferencesStorage .putString (PREFERENCE_KEY_RELEASE_DETAILS , rawReleaseDetails );
748+
749+ /* If previous release is mandatory and still processing, don't do anything right now. */
750+ if (mReleaseDetails != null && mReleaseDetails .isMandatoryUpdate ()) {
751+ if (mReleaseDetails .getId () != releaseDetails .getId ()) {
752+ MobileCenterLog .debug (LOG_TAG , "Latest release is more recent than the previous mandatory." );
753+ PreferencesStorage .putInt (PREFERENCE_KEY_DOWNLOAD_STATE , DOWNLOAD_STATE_AVAILABLE );
754+ } else {
755+ MobileCenterLog .debug (LOG_TAG , "The latest release is mandatory and already being processed." );
766756 }
767757 return ;
768- } else {
769- MobileCenterLog .debug (LOG_TAG , "Latest release is not more recent." );
770758 }
771- } catch (PackageManager .NameNotFoundException e ) {
772- MobileCenterLog .error (LOG_TAG , "Could not compare release versions." , e );
759+
760+ /* Show update dialog. */
761+ mReleaseDetails = releaseDetails ;
762+ MobileCenterLog .debug (LOG_TAG , "Latest release is more recent." );
763+ PreferencesStorage .putInt (PREFERENCE_KEY_DOWNLOAD_STATE , DOWNLOAD_STATE_AVAILABLE );
764+ if (mForegroundActivity != null ) {
765+ showUpdateDialog ();
766+ }
767+ return ;
768+ } else {
769+ MobileCenterLog .debug (LOG_TAG , "Latest release is not more recent." );
773770 }
774771 } else {
775772 MobileCenterLog .info (LOG_TAG , "This device is not compatible with the latest release." );
@@ -783,15 +780,14 @@ else if (Build.VERSION.SDK_INT >= releaseDetails.getMinApiLevel()) {
783780 /**
784781 * Check if the fetched release information should be installed.
785782 *
786- * @param packageInfo current app version.
787783 * @param releaseDetails latest release on server.
788784 * @return true if latest release on server should be used.
789785 */
790- private boolean isMoreRecent (PackageInfo packageInfo , ReleaseDetails releaseDetails ) {
791- if (releaseDetails .getVersion () == packageInfo .versionCode ) {
792- return !releaseDetails .getReleaseHash ().equals (DistributeUtils .computeReleaseHash (mContext , packageInfo ));
786+ private boolean isMoreRecent (ReleaseDetails releaseDetails ) {
787+ if (releaseDetails .getVersion () == mPackageInfo .versionCode ) {
788+ return !releaseDetails .getReleaseHash ().equals (DistributeUtils .computeReleaseHash (mPackageInfo ));
793789 }
794- return releaseDetails .getVersion () > packageInfo .versionCode ;
790+ return releaseDetails .getVersion () > mPackageInfo .versionCode ;
795791 }
796792
797793 /**
0 commit comments