Skip to content

Commit

Permalink
Merge pull request #1590 from microsoft/develop
Browse files Browse the repository at this point in the history
Version 4.4.2 (patch)
  • Loading branch information
Anastasia Senyushina committed Dec 21, 2021
2 parents 0b6ca9b + 117fad7 commit cef970c
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 18 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# App Center SDK for Android Change Log

## Version 4.4.2

### App Center

* **[Fix]** Fix print logs with `ASSERT` level.
* **[Fix]** Fix a crash during trying to get `startServiceLog` from the database after upgrading App Center SDK from the old versions.

### App Center Distribute

* **[Fix]** Fix missing required flag on Android 31 API for `PendingIntent` which is used for starting the process of installing a new release.

___

## Version 4.4.1

### App Center
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package com.microsoft.appcenter.distribute;

import static com.microsoft.appcenter.distribute.DistributeConstants.LOG_TAG;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -27,7 +29,7 @@ public class AppCenterPackageInstallerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (MY_PACKAGE_REPLACED_ACTION.equals(intent.getAction())) {
AppCenterLog.debug(AppCenterLog.LOG_TAG, "Restart application after installing a new release.");
AppCenterLog.debug(LOG_TAG, "Restart application after installing a new release.");
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(launchIntent);
Expand All @@ -37,13 +39,13 @@ public void onReceive(Context context, Intent intent) {
String message = extras.getString(PackageInstaller.EXTRA_STATUS_MESSAGE);
switch (status) {
case PackageInstaller.STATUS_PENDING_USER_ACTION:
AppCenterLog.debug(AppCenterLog.LOG_TAG, "Ask confirmation to install a new release.");
AppCenterLog.debug(LOG_TAG, "Ask confirmation to install a new release.");
Intent confirmIntent = (Intent) extras.get(Intent.EXTRA_INTENT);
confirmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(confirmIntent);
break;
case PackageInstaller.STATUS_SUCCESS:
AppCenterLog.debug(AppCenterLog.LOG_TAG, "Application was successfully updated.");
AppCenterLog.debug(LOG_TAG, "Application was successfully updated.");
break;
case PackageInstaller.STATUS_FAILURE:
case PackageInstaller.STATUS_FAILURE_ABORTED:
Expand All @@ -52,15 +54,15 @@ public void onReceive(Context context, Intent intent) {
case PackageInstaller.STATUS_FAILURE_INCOMPATIBLE:
case PackageInstaller.STATUS_FAILURE_INVALID:
case PackageInstaller.STATUS_FAILURE_STORAGE:
AppCenterLog.debug(AppCenterLog.LOG_TAG, String.format(Locale.ENGLISH, "Failed to install a new release with status: %s. Error message: %s.", status, message));
AppCenterLog.debug(LOG_TAG, String.format(Locale.ENGLISH, "Failed to install a new release with status: %s. Error message: %s.", status, message));
Toast.makeText(context, context.getString(R.string.appcenter_distribute_something_went_wrong_during_installing_new_release), Toast.LENGTH_SHORT).show();
break;
default:
AppCenterLog.debug(AppCenterLog.LOG_TAG, String.format(Locale.ENGLISH, "Unrecognized status received from installer: %s", status));
AppCenterLog.debug(LOG_TAG, String.format(Locale.ENGLISH, "Unrecognized status received from installer: %s", status));
Toast.makeText(context, context.getString(R.string.appcenter_distribute_something_went_wrong_during_installing_new_release), Toast.LENGTH_SHORT).show();
}
} else {
AppCenterLog.debug(AppCenterLog.LOG_TAG, String.format(Locale.ENGLISH, "Unrecognized action %s - do nothing.", intent.getAction()));
AppCenterLog.debug(LOG_TAG, String.format(Locale.ENGLISH, "Unrecognized action %s - do nothing.", intent.getAction()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.microsoft.appcenter.distribute;

import static android.app.PendingIntent.FLAG_MUTABLE;
import static com.microsoft.appcenter.distribute.DistributeConstants.LOG_TAG;

import android.app.PendingIntent;
Expand Down Expand Up @@ -190,11 +191,15 @@ public synchronized static void installPackage(@NonNull InputStream data, Contex
* @return IntentSender with receiver.
*/
public synchronized static IntentSender createIntentSender(Context context, int sessionId) {
int flag = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
flag = FLAG_MUTABLE;
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context,
sessionId,
new Intent(AppCenterPackageInstallerReceiver.START_ACTION),
0);
flag);
return pendingIntent.getIntentSender();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static com.microsoft.appcenter.distribute.AppCenterPackageInstallerReceiver.MY_PACKAGE_REPLACED_ACTION;
import static com.microsoft.appcenter.distribute.AppCenterPackageInstallerReceiver.START_ACTION;
import static com.microsoft.appcenter.distribute.DistributeConstants.LOG_TAG;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
Expand Down Expand Up @@ -130,7 +131,7 @@ public void onReceiveWithStartIntentWithStatusSuccess() {

/* Verify. */
verifyStatic();
AppCenterLog.debug(eq(AppCenterLog.LOG_TAG), eq("Application was successfully updated."));
AppCenterLog.debug(eq(LOG_TAG), eq("Application was successfully updated."));
}

@Test
Expand Down Expand Up @@ -179,7 +180,7 @@ private void onReceiveWithStartFailureAction(int status) {

/* Verify that log was called. */
verifyStatic();
AppCenterLog.debug(eq(AppCenterLog.LOG_TAG), eq("Failed to install a new release with status: " + status + ". Error message: null."));
AppCenterLog.debug(eq(LOG_TAG), eq("Failed to install a new release with status: " + status + ". Error message: null."));
}

@Test
Expand All @@ -198,7 +199,7 @@ public void onReceiveWithStartIntentWithUnrecognizedStatus() {

/* Verify that log was called. */
verifyStatic();
AppCenterLog.debug(eq(AppCenterLog.LOG_TAG), eq("Unrecognized status received from installer: " + status));
AppCenterLog.debug(eq(LOG_TAG), eq("Unrecognized status received from installer: " + status));
}

@Test
Expand All @@ -210,7 +211,7 @@ public void onReceiverWithUnknownAction() {

/* Verify that log was called. */
verifyStatic();
AppCenterLog.debug(eq(AppCenterLog.LOG_TAG), eq("Unrecognized action UnknownAction - do nothing."));
AppCenterLog.debug(eq(LOG_TAG), eq("Unrecognized action UnknownAction - do nothing."));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.microsoft.appcenter.distribute;

import static android.app.PendingIntent.FLAG_MUTABLE;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
Expand All @@ -27,11 +28,14 @@
import android.os.Build;
import android.provider.Settings;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;

Expand Down Expand Up @@ -186,6 +190,37 @@ public void isSystemAlertWindowsEnabledReturnsFalseIfBuildVersionQorHigherAndCan
assertFalse(InstallerUtils.isSystemAlertWindowsEnabled(mContext));
}

@Test
public void createIntentSenderOnAndroidS() throws Exception {

/* Mock SDK_INT to S. */
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.S);
createIntentSender(FLAG_MUTABLE);
}

@Test
public void createIntentSenderOnAndroidLowS() throws Exception {

/* Mock SDK_INT to M. */
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.M);
createIntentSender(0);
}

private void createIntentSender(final int expectedFlag) {
mockStatic(PendingIntent.class);
final PendingIntent mockIntent = mock(PendingIntent.class);
when(mockIntent.getIntentSender()).thenReturn(mock(IntentSender.class));
when(PendingIntent.getBroadcast(any(Context.class), anyInt(), any(Intent.class), anyInt())).then(new Answer<PendingIntent>() {
@Override
public PendingIntent answer(InvocationOnMock invocation) throws Throwable {
int flag = (int)invocation.getArguments()[3];
Assert.assertEquals(flag, expectedFlag);
return mockIntent;
}
});
InstallerUtils.createIntentSender(mContext, 1);
}

/**
* This is used ot set a specific Build.VERSION.SDK_INT for tests.
* @param field static field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.microsoft.appcenter.ingestion.models.StartServiceLog;

import org.json.JSONException;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
Expand Down Expand Up @@ -63,6 +64,36 @@ public void deserializeUnknownType() throws JSONException {
new DefaultLogSerializer().deserializeLog(payload, null);
}

@Test
public void readJsonWhenIsOneCollectorPropertyEnabledIsNull() throws JSONException {

// Prepare start service log.
StartServiceLog serviceLog = new StartServiceLog();
List<String> services = new ArrayList<>();
services.add("FIRST");
services.add("SECOND");
serviceLog.setServices(services);
UUID sid = UUID.randomUUID();
serviceLog.setSid(sid);
serviceLog.setTimestamp(new Date());

// Prepare log container.
ArrayList<Log> logArrayList = new ArrayList<>();
logArrayList.add(serviceLog);
LogContainer serializerContainer = new LogContainer();
serializerContainer.setLogs(logArrayList);

// Serializer log container.
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(StartServiceLog.TYPE, new StartServiceLogFactory());
String payload = serializer.serializeContainer(serializerContainer);

// Deserialize log container.
LogContainer deserializeContainer = serializer.deserializeContainer(payload, null);
StartServiceLog deserializeStartServiceLog = (StartServiceLog) deserializeContainer.getLogs().get(0);
Assert.assertNull(deserializeStartServiceLog.isOneCollectorEnabled());
}

@Test
public void startServiceLog() throws JSONException {
StartServiceLog log = new StartServiceLog();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class StartServiceLog extends AbstractLog {
/**
* OneCollector usage status.
*/
private boolean isOneCollectorEnabled = false;
private Boolean isOneCollectorEnabled = null;

/**
* The list of services of the AppCenter start call.
Expand Down Expand Up @@ -61,11 +61,11 @@ public void setServices(List<String> services) {
this.services = services;
}

public void oneCollectorEnabled(boolean isEnabled) {
public void oneCollectorEnabled(Boolean isEnabled) {
isOneCollectorEnabled = isEnabled;
}

public boolean isOneCollectorEnabled() {
public Boolean isOneCollectorEnabled() {
return isOneCollectorEnabled;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,33 @@ public static void error(@NonNull String tag, @NonNull String message, Throwable
}
}

/**
* Log a message with level ASSERT
*
* @param tag the log tag for your message
* @param message the log message
*/
@SuppressWarnings("WeakerAccess")
public static void logAssert(@NonNull String tag, @NonNull String message) {
if (sLogLevel <= Log.ASSERT) {
Log.println(Log.ASSERT, tag, message);
}
}

/**
* Log a message with level ASSERT
*
* @param tag the log tag for your message
* @param message the log message
* @param throwable the throwable you want to log
*/
@SuppressWarnings({"WeakerAccess", "SameParameterValue"})
public static void logAssert(@NonNull String tag, @NonNull String message, Throwable throwable) {
if (sLogLevel <= Log.ASSERT) {
Log.println(Log.ASSERT, tag, message + "\n" + Log.getStackTraceString(throwable));
}
}

private static String getMessageWithTag(String tag, String message) {
return String.format("%s: %s", tag, message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
public class AppCenterLogTest {

private static void callLogs() {
AppCenterLog.logAssert("my-tag", "error with my-tag");
AppCenterLog.logAssert("my-tag", "error with my-tag with exception", new Exception());
AppCenterLog.error("my-tag", "error with my-tag");
AppCenterLog.error("my-tag", "error with my-tag with exception", new Exception());
AppCenterLog.warn("my-tag", "warn with my-tag");
Expand Down
8 changes: 4 additions & 4 deletions versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// Version constants

ext {
versionCode = 65
versionName = '4.4.1'
versionCode = 66
versionName = '4.4.2'
minSdkVersion = 21
compileSdkVersion = 30
targetSdkVersion = 30
compileSdkVersion = 31
targetSdkVersion = 31
annotationVersion = '1.2.0'
}

0 comments on commit cef970c

Please sign in to comment.