/** * 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(); }
@Override public void clearPendingLogState() { mPendingDbIdentifiers.clear(); mPendingDbIdentifiersGroups.clear(); AppCenterLog.debug(LOG_TAG, "Cleared pending log states"); }
/** * Handle network available update on API level >= 21. */ private synchronized void onNetworkAvailable(Network network) { AppCenterLog.debug(LOG_TAG, "Network available netId: " + network); mAvailableNetworks.add(network); AppCenterLog.debug(LOG_TAG, "Available networks netIds: " + mAvailableNetworks); /* * Trigger event only once if we gain a new network while one was already * available. Special logic is handled in network lost events. */ if (mAvailableNetworks.size() == 1) { notifyNetworkStateUpdated(true); } }
/** * Call this whenever an activity is resumed to update session tracker state. */ public void onActivityResumed() { /* Record resume time for session timeout management. */ AppCenterLog.debug(Analytics.LOG_TAG, "onActivityResumed"); mLastResumedTime = SystemClock.elapsedRealtime(); sendStartSessionIfNeeded(); }
@Override public void run() { mChannel.shutdown(); AppCenterLog.debug(AppCenter.LOG_TAG, "Channel completed shutdown."); semaphore.release(); } });
@Override public synchronized void close() { if (mTasks.size() > 0) { AppCenterLog.debug(LOG_TAG, "Cancelling " + mTasks.size() + " network call(s)."); for (DefaultHttpClientCallTask task : mTasks) { task.cancel(true); } mTasks.clear(); } }
/** * Store a flag for failure to enable updates from the tester apps, to later reattempt using the browser update setup. */ synchronized void storeTesterAppUpdateSetupFailedParameter(@NonNull String requestId, @NonNull String testerAppUpdateSetupFailed) { /* Keep redirection parameters for later if we are not started and enabled yet. */ if (mContext == null) { AppCenterLog.debug(LOG_TAG, "Tester app update setup failed parameter received before onStart, keep it in memory."); mBeforeStartRequestId = requestId; mBeforeStartTesterAppUpdateSetupFailed = testerAppUpdateSetupFailed; } else if (requestId.equals(SharedPreferencesManager.getString(PREFERENCE_KEY_REQUEST_ID))) { AppCenterLog.debug(LOG_TAG, "Stored tester app update setup failed parameter."); SharedPreferencesManager.putString(PREFERENCE_KEY_TESTER_APP_UPDATE_SETUP_FAILED_MESSAGE_KEY, testerAppUpdateSetupFailed); } }
/** * Store update setup failure message used later to show in setup failure dialog for user. */ synchronized void storeUpdateSetupFailedParameter(@NonNull String requestId, @NonNull String updateSetupFailed) { /* Keep redirection parameters for later if we are not started and enabled yet. */ if (mContext == null) { AppCenterLog.debug(LOG_TAG, "Update setup failed parameter received before onStart, keep it in memory."); mBeforeStartRequestId = requestId; mBeforeStartUpdateSetupFailed = updateSetupFailed; } else if (requestId.equals(SharedPreferencesManager.getString(PREFERENCE_KEY_REQUEST_ID))) { AppCenterLog.debug(LOG_TAG, "Stored update setup failed parameter."); SharedPreferencesManager.putString(PREFERENCE_KEY_UPDATE_SETUP_FAILED_MESSAGE_KEY, updateSetupFailed); } }
@Override public synchronized void onNetworkStateUpdated(boolean connected) { if (connected && mCalls.size() > 0) { AppCenterLog.debug(LOG_TAG, "Network is available. " + mCalls.size() + " pending call(s) to submit now."); for (Call call : mCalls) { call.run(); } mCalls.clear(); } /* * Loss of network may not lead to call failure. Also, cancelling the ongoing requests * can cause requests duplication, so let it fail and then retry when we're online again. */ }
/** * Cancel notification if needed. */ private synchronized void cancelNotification() { if (getStoredDownloadState() == DOWNLOAD_STATE_NOTIFIED) { AppCenterLog.debug(LOG_TAG, "Delete notification"); NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); //noinspection ConstantConditions notificationManager.cancel(DistributeUtils.getNotificationId()); } }
/** * Enable firebase analytics collection. * * @param context the context to retrieve FirebaseAnalytics instance. */ @SuppressWarnings("WeakerAccess") public static void enableFirebaseAnalytics(@NonNull Context context) { AppCenterLog.debug(LOG_TAG, "Enabling Firebase analytics collection."); getInstance().setFirebaseAnalyticsEnabled(context, true); }
@Override public synchronized void removeGroup(String groupName) { AppCenterLog.debug(LOG_TAG, "removeGroup(" + groupName + ")"); GroupState groupState = mGroupStates.remove(groupName); if (groupState != null) { cancelTimer(groupState); } /* Call listeners so that they can react on group removed. */ for (Listener listener : mListeners) { listener.onGroupRemoved(groupName); } }
@SuppressWarnings("MissingPermission") static void setAnalyticsEnabled(@NonNull Context context, boolean enabled) { try { FirebaseAnalytics.getInstance(context).setAnalyticsCollectionEnabled(enabled); } catch (LinkageError e) { AppCenterLog.debug(LOG_TAG, "Firebase analytics not available so cannot change state."); } }
/** * Set context, used when need to manipulate context before onStarted. * For example when download completes after application process exited. */ synchronized ReleaseDetails startFromBackground(Context context) { if (mAppSecret == null) { AppCenterLog.debug(LOG_TAG, "Called before onStart, init storage"); mContext = context; SharedPreferencesManager.initialize(mContext); mMobileCenterPreferenceStorage = mContext.getSharedPreferences(PREFERENCES_NAME_MOBILE_CENTER, Context.MODE_PRIVATE); mReleaseDetails = DistributeUtils.loadCachedReleaseDetails(); } return mReleaseDetails; }
@Override public synchronized void onStarted(@NonNull Context context, @NonNull Channel channel, String appSecret, String transmissionTargetToken, boolean startedFromApp) { mContext = context; super.onStarted(context, channel, appSecret, transmissionTargetToken, startedFromApp); if (FirebaseUtils.isFirebaseAvailable() && !mFirebaseAnalyticsEnabled) { AppCenterLog.debug(LOG_TAG, "Disabling Firebase analytics collection by default."); setFirebaseAnalyticsEnabled(context, false); } }
@Override public synchronized ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, ServiceCallback serviceCallback) { Call call = new Call(mDecoratedApi, url, method, headers, callTemplate, serviceCallback); if (mNetworkStateHelper.isNetworkConnected()) { call.run(); } else { mCalls.add(call); AppCenterLog.debug(LOG_TAG, "Call triggered with no network connectivity, waiting network to become available..."); } return call; }
@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"); } }
private boolean startService(AppCenterService serviceInstance, Collection<AppCenterService> startedServices) { String serviceName = serviceInstance.getServiceName(); if (ServiceInstrumentationUtils.isServiceDisabledByInstrumentation(serviceName)) { AppCenterLog.debug(LOG_TAG, "Instrumentation variable to disable service has been set; not starting service " + serviceName + "."); return false; } else { serviceInstance.onStarting(mAppCenterHandler); mApplication.registerActivityLifecycleCallbacks(serviceInstance); mServices.add(serviceInstance); startedServices.add(serviceInstance); return true; } }
/** * "Ask me in a day" postpone action. * * @param releaseDetails release details. */ private synchronized void postponeRelease(ReleaseDetails releaseDetails) { if (releaseDetails == mReleaseDetails) { AppCenterLog.debug(LOG_TAG, "Postpone updates for a day."); SharedPreferencesManager.putLong(PREFERENCE_KEY_POSTPONE_TIME, System.currentTimeMillis()); completeWorkflow(); } else { showDisabledToast(); } }
@Test public void noCrashInLastSession() { mockStatic(ErrorLogHelper.class); when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(null); when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[0]); when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]); assertNull(Crashes.getLastSessionCrashReport().get()); Crashes.getInstance().onStarted(mock(Context.class), mock(Channel.class), "", null, true); assertFalse(Crashes.hasCrashedInLastSession().get()); assertNull(Crashes.getLastSessionCrashReport().get()); verifyStatic(never()); AppCenterLog.debug(anyString(), anyString()); }