/** * Common validation for both AppCenter and One Collector, specific validation happens later. */ private boolean isValidValue(Object value) { if (value == null) { AppCenterLog.error(LOG_TAG, VALUE_NULL_ERROR_MESSAGE); return false; } return true; } }
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"); AppCenterLog.warn("my-tag", "warn with my-tag with exception", new Exception()); AppCenterLog.info("my-tag", "info with my-tag"); AppCenterLog.info("my-tag", "info with my-tag with exception", new Exception()); AppCenterLog.debug("my-tag", "debug with my-tag"); AppCenterLog.debug("my-tag", "debug with my-tag with exception", new Exception()); AppCenterLog.verbose("my-tag", "verbose with my-tag"); AppCenterLog.verbose("my-tag", "verbose with my-tag with exception", new Exception()); }
@Override public void onBeforeCalling(URL url, Map<String, String> headers) { if (AppCenterLog.getLogLevel() <= VERBOSE) { /* Log url. */ AppCenterLog.verbose(LOG_TAG, "Calling " + url + "..."); /* Log headers. */ Map<String, String> logHeaders = new HashMap<>(headers); String appSecret = logHeaders.get(APP_SECRET); if (appSecret != null) { logHeaders.put(APP_SECRET, HttpUtils.hideSecret(appSecret)); } AppCenterLog.verbose(LOG_TAG, "Headers: " + logHeaders); } } }
/** * Common validation for both AppCenter and One Collector, specific validation happens later. */ private boolean isValidKey(String key) { if (key == null) { AppCenterLog.error(LOG_TAG, "Property key must not be null"); return false; } if (mProperties.containsKey(key)) { AppCenterLog.warn(LOG_TAG, "Property \"" + key + "\" is already set and will be overridden."); } return true; }
@Override protected synchronized void applyEnabledState(boolean enabled) { initialize(); if (!enabled) { for (File file : ErrorLogHelper.getErrorStorageDirectory().listFiles()) { AppCenterLog.debug(LOG_TAG, "Deleting file " + file); if (!file.delete()) { AppCenterLog.warn(LOG_TAG, "Failed to delete file " + file); } } AppCenterLog.info(LOG_TAG, "Deleted crashes local files"); } }
/** * Call this whenever an activity is paused to update session tracker state. */ public void onActivityPaused() { /* Record pause time for session timeout management. */ AppCenterLog.debug(Analytics.LOG_TAG, "onActivityPaused"); mLastPausedTime = SystemClock.elapsedRealtime(); }
/** * Get persistence priority flag. * * @param flags All flags to extract persistence priority from. * @param warnFallback If true and falling back from an invalid value: print a warning. * @return persistence Priority flag for persistence. */ public static int getPersistenceFlag(int flags, boolean warnFallback) { int persistencePriority = flags & PERSISTENCE_MASK; if (persistencePriority != PERSISTENCE_NORMAL && persistencePriority != PERSISTENCE_CRITICAL) { if (persistencePriority != 0 && warnFallback) { AppCenterLog.warn(LOG_TAG, "Invalid value=" + persistencePriority + " for persistence flag, using NORMAL as a default."); } persistencePriority = PERSISTENCE_NORMAL; } return persistencePriority; } }
/** * Gets SQLite database. * * @return SQLite database. * @throws RuntimeException if an error occurs. */ @VisibleForTesting SQLiteDatabase getDatabase() { /* Try opening database. */ try { return mSQLiteOpenHelper.getWritableDatabase(); } catch (RuntimeException e) { AppCenterLog.warn(LOG_TAG, "Failed to open database. Trying to delete database (may be corrupted).", e); /* First error, try to delete database (may be corrupted). */ if (mContext.deleteDatabase(mDatabase)) { AppCenterLog.info(LOG_TAG, "The database was successfully deleted."); } else { AppCenterLog.warn(LOG_TAG, "Failed to delete database."); } /* Retry, let exception thrown if it fails this time. */ return mSQLiteOpenHelper.getWritableDatabase(); } }
/** * Register application for push without Firebase. */ private synchronized void registerPushTokenWithoutFirebase() { if (mSenderId == null) { int resId = mContext.getResources().getIdentifier("gcm_defaultSenderId", "string", mContext.getPackageName()); try { mSenderId = mContext.getString(resId); } catch (Resources.NotFoundException e) { AppCenterLog.error(LOG_TAG, "Push.setSenderId was not called, aborting registration."); return; } } Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); registrationIntent.setPackage("com.google.android.gsf"); registrationIntent.putExtra("sender", mSenderId); registrationIntent.putExtra("app", PendingIntent.getBroadcast(mContext, 0, new Intent(), 0)); try { mContext.startService(registrationIntent); } catch (IllegalStateException e) { AppCenterLog.info(LOG_TAG, "Cannot register in background, will wait to be in foreground"); mTokenNeedsRegistrationInForeground = true; } catch (RuntimeException e) { AppCenterLog.error(LOG_TAG, "Failed to register push token", e); } }
code = errorDetails.getCode(); } catch (JSONException je) { AppCenterLog.verbose(LOG_TAG, "Cannot read the error as JSON", je); AppCenterLog.info(LOG_TAG, "No release available to the current user."); } else { AppCenterLog.error(LOG_TAG, "Failed to check latest release:", e); SharedPreferencesManager.remove(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID); SharedPreferencesManager.remove(PREFERENCE_KEY_UPDATE_TOKEN);
/** * Get configuration. */ private synchronized void getConfiguration(final int configurationUrlIndex, final String rumKey, final HttpClient httpClient) { /* Check if a disable happened while we were waiting the remote configuration. */ if (httpClient != mHttpClient) { return; } if (configurationUrlIndex >= CONFIGURATION_ENDPOINTS.length) { AppCenterLog.error(LOG_TAG, "Could not get configuration file from any of the endpoints."); return; } final String url = CONFIGURATION_ENDPOINTS[configurationUrlIndex]; AppCenterLog.verbose(LOG_TAG, "Calling " + url); httpClient.callAsync(url, METHOD_GET, HEADERS, null, new ServiceCallback() { @Override public void onCallSucceeded(String payload) { /* Read JSON configuration and start testing. */ handleRemoteConfiguration(httpClient, rumKey, payload); } @Override public void onCallFailed(Exception e) { /* Log error and try the next configuration endpoint. */ AppCenterLog.error(LOG_TAG, "Could not get configuration file at " + url, e); getConfiguration(configurationUrlIndex + 1, rumKey, httpClient); } }); }
@Override public void onCallSucceeded(String payload) { AppCenterLog.info(LOG_TAG, "Measurements reported successfully."); }
/** * Return log level filter for logs coming from this SDK. * * @return log level as defined by {@link android.util.Log}. */ @IntRange(from = VERBOSE, to = NONE) public static int getLogLevel() { return AppCenterLog.getLogLevel(); }
/** * Check for push message clicked from notification center in activity intent. * * @param intent intent to inspect. */ private synchronized void checkPushInIntent(Activity activity, Intent intent) { if (mInstanceListener != null) { String googleMessageId = PushIntentUtils.getMessageId(intent); if (googleMessageId != null && !googleMessageId.equals(mLastGoogleMessageId) && !googleMessageId.equals(mFirstGoogleMessageId)) { if (mFirstGoogleMessageId == null) { mFirstGoogleMessageId = googleMessageId; } PushNotification notification = new PushNotification(intent); AppCenterLog.info(LOG_TAG, "Clicked push message from background id=" + googleMessageId); mLastGoogleMessageId = googleMessageId; AppCenterLog.debug(LOG_TAG, "Push intent extras=" + intent.getExtras()); mInstanceListener.onPushNotificationReceived(activity, notification); } } }
@Test public void receivedPushInBackgroundWithoutFirebaseWithoutDebugLog() { when(AppCenterLog.getLogLevel()).thenReturn(Log.INFO); IllegalStateException exception = new IllegalStateException(); when(FirebaseInstanceId.getInstance()).thenThrow(exception); Intent pushIntent = mock(Intent.class); Bundle extras = mock(Bundle.class); when(pushIntent.getExtras()).thenReturn(extras); when(extras.keySet()).thenReturn(Sets.newSet("key1")); when(extras.get("key1")).thenReturn("val1"); Push.getInstance().onMessageReceived(mContext, pushIntent); verifyStatic(); PushNotifier.handleNotification(any(Context.class), same(pushIntent)); verifyStatic(never()); AppCenterLog.debug(eq(Push.LOG_TAG), argThat(new ArgumentMatcher<String>() { @Override public boolean matches(Object argument) { return argument.toString().contains("key1=val1"); } })); }
@Test @SuppressWarnings("unchecked") public void onBeforeCallingWithAnotherLogLevel() throws Exception { /* Mock instances. */ String apiKey = UUIDUtils.randomUUID().toString(); HttpClient.CallTemplate callTemplate = getCallTemplate(); /* Change log level. */ AppCenterLog.setLogLevel(android.util.Log.WARN); /* Call onBeforeCalling with parameters. */ callTemplate.onBeforeCalling(mock(URL.class), mock(Map.class)); /* Verify. */ verifyStatic(never()); AppCenterLog.verbose(anyString(), anyString()); }
/** * Used to avoid querying download manager on every activity change. * * @param releaseDetails release details to check state. */ synchronized void markDownloadStillInProgress(ReleaseDetails releaseDetails) { if (releaseDetails == mReleaseDetails) { AppCenterLog.verbose(LOG_TAG, "Download is still in progress..."); mCheckedDownload = true; } }
private boolean isValidKey(String key) { if (key == null || !KEY_PATTERN.matcher(key).matches()) { AppCenterLog.error(LOG_TAG, "Custom property \"" + key + "\" must match \"" + KEY_PATTERN + "\""); return false; } if (key.length() > MAX_PROPERTY_KEY_LENGTH) { AppCenterLog.error(LOG_TAG, "Custom property \"" + key + "\" length cannot be longer than " + MAX_PROPERTY_KEY_LENGTH + " characters."); return false; } if (mProperties.containsKey(key)) { AppCenterLog.warn(LOG_TAG, "Custom property \"" + key + "\" is already set or cleared and will be overridden."); } return true; }
@Override public void clearPendingLogState() { mPendingDbIdentifiers.clear(); mPendingDbIdentifiersGroups.clear(); AppCenterLog.debug(LOG_TAG, "Cleared pending log states"); }
/** * Generates crash for test purpose. */ public static void generateTestCrash() { if (Constants.APPLICATION_DEBUGGABLE) { throw new TestCrashException(); } else { AppCenterLog.warn(LOG_TAG, "The application is not debuggable so SDK won't generate test crash"); } }