|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.appdistribution; |
| 16 | + |
| 17 | +import static com.google.firebase.appdistribution.FirebaseAppDistributionException.Status.NETWORK_FAILURE; |
| 18 | +import static com.google.firebase.appdistribution.TaskUtils.safeSetTaskException; |
| 19 | + |
| 20 | +import android.app.Activity; |
| 21 | +import android.content.Intent; |
| 22 | +import android.net.Uri; |
| 23 | +import androidx.annotation.GuardedBy; |
| 24 | +import androidx.annotation.NonNull; |
| 25 | +import androidx.annotation.Nullable; |
| 26 | +import com.google.firebase.appdistribution.internal.AppDistributionReleaseInternal; |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.URL; |
| 29 | +import java.util.concurrent.Executor; |
| 30 | +import java.util.concurrent.Executors; |
| 31 | +import javax.net.ssl.HttpsURLConnection; |
| 32 | + |
| 33 | +/** Client class that handles updateApp functionality for AABs in {@link UpdateAppClient}. */ |
| 34 | +class UpdateAabClient { |
| 35 | + private static final String TAG = "UpdateAabClient:"; |
| 36 | + |
| 37 | + private final Executor updateExecutor; |
| 38 | + |
| 39 | + @GuardedBy("updateAabLock") |
| 40 | + private UpdateTaskImpl cachedUpdateTask; |
| 41 | + |
| 42 | + @GuardedBy("updateAabLock") |
| 43 | + private Activity currentActivity; |
| 44 | + |
| 45 | + @GuardedBy("updateAabLock") |
| 46 | + private AppDistributionReleaseInternal aabReleaseInProgress; |
| 47 | + |
| 48 | + private final Object updateAabLock = new Object(); |
| 49 | + |
| 50 | + public UpdateAabClient() { |
| 51 | + this(Executors.newSingleThreadExecutor()); |
| 52 | + } |
| 53 | + |
| 54 | + public UpdateAabClient(@NonNull Executor updateExecutor) { |
| 55 | + this.updateExecutor = updateExecutor; |
| 56 | + } |
| 57 | + |
| 58 | + public UpdateTaskImpl updateAab(@NonNull AppDistributionReleaseInternal newRelease) { |
| 59 | + synchronized (updateAabLock) { |
| 60 | + if (cachedUpdateTask != null && !cachedUpdateTask.isComplete()) { |
| 61 | + return cachedUpdateTask; |
| 62 | + } |
| 63 | + |
| 64 | + cachedUpdateTask = new UpdateTaskImpl(); |
| 65 | + aabReleaseInProgress = newRelease; |
| 66 | + redirectToPlayForAabUpdate(newRelease.getDownloadUrl()); |
| 67 | + |
| 68 | + return cachedUpdateTask; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + HttpsURLConnection openHttpsUrlConnection(String downloadUrl) |
| 73 | + throws FirebaseAppDistributionException { |
| 74 | + HttpsURLConnection httpsURLConnection; |
| 75 | + |
| 76 | + try { |
| 77 | + URL url = new URL(downloadUrl); |
| 78 | + httpsURLConnection = (HttpsURLConnection) url.openConnection(); |
| 79 | + } catch (IOException e) { |
| 80 | + throw new FirebaseAppDistributionException( |
| 81 | + Constants.ErrorMessages.NETWORK_ERROR, NETWORK_FAILURE, e); |
| 82 | + } |
| 83 | + return httpsURLConnection; |
| 84 | + } |
| 85 | + |
| 86 | + private void redirectToPlayForAabUpdate(String downloadUrl) { |
| 87 | + synchronized (updateAabLock) { |
| 88 | + if (currentActivity == null) { |
| 89 | + safeSetTaskException( |
| 90 | + cachedUpdateTask, |
| 91 | + new FirebaseAppDistributionException( |
| 92 | + Constants.ErrorMessages.APP_BACKGROUNDED, |
| 93 | + FirebaseAppDistributionException.Status.DOWNLOAD_FAILURE)); |
| 94 | + return; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // The 302 redirect is obtained here to open the play store directly and avoid opening chrome |
| 99 | + updateExecutor.execute( |
| 100 | + () -> { |
| 101 | + HttpsURLConnection connection; |
| 102 | + String redirect; |
| 103 | + try { |
| 104 | + connection = openHttpsUrlConnection(downloadUrl); |
| 105 | + |
| 106 | + // To get url to play without redirect we do this connection |
| 107 | + connection.setInstanceFollowRedirects(false); |
| 108 | + redirect = connection.getHeaderField("Location"); |
| 109 | + connection.disconnect(); |
| 110 | + connection.getInputStream().close(); |
| 111 | + } catch (FirebaseAppDistributionException | IOException e) { |
| 112 | + setUpdateTaskCompletionErrorWithDefault( |
| 113 | + e, |
| 114 | + new FirebaseAppDistributionException( |
| 115 | + Constants.ErrorMessages.NETWORK_ERROR, |
| 116 | + FirebaseAppDistributionException.Status.DOWNLOAD_FAILURE)); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + if (!redirect.isEmpty()) { |
| 121 | + Intent updateIntent = new Intent(Intent.ACTION_VIEW); |
| 122 | + updateIntent.setData(Uri.parse(redirect)); |
| 123 | + updateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 124 | + LogWrapper.getInstance().v(TAG + "Redirecting to play"); |
| 125 | + |
| 126 | + synchronized (updateAabLock) { |
| 127 | + currentActivity.startActivity(updateIntent); |
| 128 | + cachedUpdateTask.updateProgress( |
| 129 | + UpdateProgress.builder() |
| 130 | + .setApkBytesDownloaded(-1) |
| 131 | + .setApkFileTotalBytes(-1) |
| 132 | + .setUpdateStatus(UpdateStatus.REDIRECTED_TO_PLAY) |
| 133 | + .build()); |
| 134 | + } |
| 135 | + } else { |
| 136 | + setUpdateTaskCompletionError( |
| 137 | + new FirebaseAppDistributionException( |
| 138 | + Constants.ErrorMessages.NETWORK_ERROR, |
| 139 | + FirebaseAppDistributionException.Status.DOWNLOAD_FAILURE)); |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + void setCurrentActivity(@Nullable Activity activity) { |
| 145 | + synchronized (updateAabLock) { |
| 146 | + this.currentActivity = activity; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void setUpdateTaskCompletionError(FirebaseAppDistributionException e) { |
| 151 | + synchronized (updateAabLock) { |
| 152 | + safeSetTaskException(cachedUpdateTask, e); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private void setUpdateTaskCompletionErrorWithDefault( |
| 157 | + Exception e, FirebaseAppDistributionException defaultFirebaseException) { |
| 158 | + if (e instanceof FirebaseAppDistributionException) { |
| 159 | + setUpdateTaskCompletionError((FirebaseAppDistributionException) e); |
| 160 | + } else { |
| 161 | + setUpdateTaskCompletionError(defaultFirebaseException); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + void tryCancelAabUpdateTask() { |
| 166 | + synchronized (updateAabLock) { |
| 167 | + safeSetTaskException( |
| 168 | + cachedUpdateTask, |
| 169 | + new FirebaseAppDistributionException( |
| 170 | + Constants.ErrorMessages.UPDATE_CANCELED, |
| 171 | + FirebaseAppDistributionException.Status.INSTALLATION_CANCELED, |
| 172 | + ReleaseUtils.convertToAppDistributionRelease(aabReleaseInProgress))); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments