/** * Implements {@link #getMinidumpDirectory()} at instance level. */ private synchronized AppCenterFuture<String> getNewMinidumpDirectoryAsync() { final DefaultAppCenterFuture<String> future = new DefaultAppCenterFuture<>(); postAsyncGetter(new Runnable() { @Override public void run() { future.complete(ErrorLogHelper.getNewMinidumpDirectory().getAbsolutePath()); } }, future, null); return future; }
/** * Implements {@link #hasCrashedInLastSession()} at instance level. */ private synchronized AppCenterFuture<Boolean> hasInstanceCrashedInLastSession() { final DefaultAppCenterFuture<Boolean> future = new DefaultAppCenterFuture<>(); postAsyncGetter(new Runnable() { @Override public void run() { future.complete(mLastSessionErrorReport != null); } }, future, false); return future; }
/** * Implements {@link #getLastSessionCrashReport()} at instance level. */ private synchronized AppCenterFuture<ErrorReport> getInstanceLastSessionCrashReport() { final DefaultAppCenterFuture<ErrorReport> future = new DefaultAppCenterFuture<>(); postAsyncGetter(new Runnable() { @Override public void run() { future.complete(mLastSessionErrorReport); } }, future, null); return future; }
/** * Implementation of {@link WrapperSdkExceptionManager#getUnprocessedErrorReports()}. */ AppCenterFuture<Collection<ErrorReport>> getUnprocessedErrorReports() { final DefaultAppCenterFuture<Collection<ErrorReport>> future = new DefaultAppCenterFuture<>(); postAsyncGetter(new Runnable() { @Override public void run() { Collection<ErrorReport> reports = new ArrayList<>(mUnprocessedErrorReports.size()); for (ErrorLogReport entry : mUnprocessedErrorReports.values()) { reports.add(entry.report); } future.complete(reports); } }, future, Collections.<ErrorReport>emptyList()); return future; }
/** * Help implementing static isEnabled() for services with future. * * @return future with result being <code>true</code> if enabled, <code>false</code> otherwise. */ protected synchronized AppCenterFuture<Boolean> isInstanceEnabledAsync() { final DefaultAppCenterFuture<Boolean> future = new DefaultAppCenterFuture<>(); postAsyncGetter(new Runnable() { @Override public void run() { future.complete(true); } }, future, false); return future; }
/** * Implementation of {@link WrapperSdkExceptionManager#sendCrashReportsOrAwaitUserConfirmation(Collection)}. */ AppCenterFuture<Boolean> sendCrashReportsOrAwaitUserConfirmation(final Collection<String> filteredReportIds) { final DefaultAppCenterFuture<Boolean> future = new DefaultAppCenterFuture<>(); postAsyncGetter(new Runnable() { @Override public void run() { /* Apply the filtering. */ Iterator<Map.Entry<UUID, ErrorLogReport>> iterator = mUnprocessedErrorReports.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<UUID, ErrorLogReport> entry = iterator.next(); UUID id = entry.getKey(); String idString = entry.getValue().report.getId(); if (filteredReportIds != null && filteredReportIds.contains(idString)) { AppCenterLog.debug(LOG_TAG, "CrashesListener.shouldProcess returned true, continue processing log: " + idString); } else { AppCenterLog.debug(LOG_TAG, "CrashesListener.shouldProcess returned false, clean up and ignore log: " + idString); removeAllStoredErrorLogFiles(id); iterator.remove(); } } /* Proceed to check if user confirmation is needed. */ future.complete(sendCrashReportsOrAwaitUserConfirmation()); } }, future, false); return future; }
/** * Implements {@link #setEnabled(boolean)}}. */ private synchronized AppCenterFuture<Void> setInstanceEnabledAsync(final boolean enabled) { final DefaultAppCenterFuture<Void> future = new DefaultAppCenterFuture<>(); if (checkPrecondition()) { mHandler.post(new Runnable() { @Override public void run() { setInstanceEnabled(enabled); future.complete(null); } }); } else { future.complete(null); } return future; }
/** * Check whether this target is enabled or not. * * @return future with result being <code>true</code> if enabled, <code>false</code> otherwise. * @see AppCenterFuture */ public AppCenterFuture<Boolean> isEnabledAsync() { final DefaultAppCenterFuture<Boolean> future = new DefaultAppCenterFuture<>(); Analytics.getInstance().postCommand(new Runnable() { @Override public void run() { future.complete(isEnabled()); } }, future, false); return future; }
final DefaultAppCenterFuture<Void> future = new DefaultAppCenterFuture<>(); final Runnable coreDisabledRunnable = new Runnable() {
final DefaultAppCenterFuture<Void> future = new DefaultAppCenterFuture<>(); Analytics.getInstance().postCommand(new Runnable() {
/** * Implements {@link #isEnabled()} at instance level. */ private synchronized AppCenterFuture<Boolean> isInstanceEnabledAsync() { final DefaultAppCenterFuture<Boolean> future = new DefaultAppCenterFuture<>(); if (checkPrecondition()) { mAppCenterHandler.post(new Runnable() { @Override public void run() { future.complete(true); } }, new Runnable() { @Override public void run() { future.complete(false); } }); } else { future.complete(false); } return future; }
/** * Implements {@link #getInstallId()}. */ private synchronized AppCenterFuture<UUID> getInstanceInstallIdAsync() { final DefaultAppCenterFuture<UUID> future = new DefaultAppCenterFuture<>(); if (checkPrecondition()) { mAppCenterHandler.post(new Runnable() { @Override public void run() { future.complete(IdHelper.getInstallId()); } }, new Runnable() { @Override public void run() { future.complete(null); } }); } else { future.complete(null); } return future; }
@Test public void getWithInterruption() throws InterruptedException { final DefaultAppCenterFuture<Boolean> future = new DefaultAppCenterFuture<>(); final AtomicReference<Boolean> result = new AtomicReference<>(); Thread thread = new Thread() { @Override public void run() { result.set(future.get()); } }; thread.start(); thread.interrupt(); future.complete(true); thread.join(); assertEquals(true, result.get()); }
static AppCenterFuture<UUID> getInstallId() { final DefaultAppCenterFuture<UUID> future = new DefaultAppCenterFuture<>(); getInstance().post(new Runnable() { @Override public void run() { future.complete(mInstallId); } }); return future; }
@Test @PrepareForTest(DefaultAppCenterFuture.class) public void isDoneWithInterruption() throws Exception { CountDownLatch latch = mock(CountDownLatch.class); whenNew(CountDownLatch.class).withAnyArguments().thenReturn(latch); when(latch.await(anyLong(), any(TimeUnit.class))).thenThrow(new InterruptedException()).thenReturn(true); final DefaultAppCenterFuture<Boolean> future = new DefaultAppCenterFuture<>(); final AtomicReference<Boolean> result = new AtomicReference<>(); Thread thread = new Thread() { @Override public void run() { result.set(future.isDone()); } }; thread.start(); thread.join(); assertEquals(true, result.get()); }
@Test public void completeTwiceIgnored() { DefaultAppCenterFuture<Integer> future = new DefaultAppCenterFuture<>(); future.complete(1); future.complete(2); assertEquals(Integer.valueOf(1), future.get()); }
/** * {@link #setMaxStorageSize(long)} implementation at instance level. * * @param storageSizeInBytes size to set SQLite database to in bytes. * @return future of result of set maximum storage size. */ private synchronized AppCenterFuture<Boolean> setInstanceMaxStorageSizeAsync(long storageSizeInBytes) { DefaultAppCenterFuture<Boolean> setMaxStorageSizeFuture = new DefaultAppCenterFuture<>(); if (mConfiguredFromApp) { AppCenterLog.error(LOG_TAG, "setMaxStorageSize may not be called after App Center has been configured."); setMaxStorageSizeFuture.complete(false); return setMaxStorageSizeFuture; } if (storageSizeInBytes < MINIMUM_STORAGE_SIZE) { AppCenterLog.error(LOG_TAG, "Maximum storage size must be at least " + MINIMUM_STORAGE_SIZE + " bytes."); setMaxStorageSizeFuture.complete(false); return setMaxStorageSizeFuture; } if (mSetMaxStorageSizeFuture != null) { AppCenterLog.error(LOG_TAG, "setMaxStorageSize may only be called once per app launch."); setMaxStorageSizeFuture.complete(false); return setMaxStorageSizeFuture; } mMaxStorageSizeInBytes = storageSizeInBytes; mSetMaxStorageSizeFuture = setMaxStorageSizeFuture; return setMaxStorageSizeFuture; }
@Test @PrepareForTest(HandlerUtils.class) public void multipleCallbacks() { mockStatic(HandlerUtils.class); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) { ((Runnable) invocation.getArguments()[0]).run(); return null; } }).when(HandlerUtils.class); HandlerUtils.runOnUiThread(any(Runnable.class)); DefaultAppCenterFuture<Integer> future = new DefaultAppCenterFuture<>(); @SuppressWarnings("unchecked") AppCenterConsumer<Integer> function = mock(AppCenterConsumer.class); future.thenAccept(function); future.thenAccept(function); future.complete(1); verify(function, times(2)).accept(1); /* Works also after completion. */ future.thenAccept(function); future.thenAccept(function); verify(function, times(4)).accept(1); } }