Search in sources :

Example 6 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class NotificationPlatformBridge method makePlatformTag.

/**
     * Generates the tag to be passed to the notification manager.
     *
     * If the generated tag is the same as that of a previous notification, a new notification shown
     * with this tag will replace it.
     *
     * If the input tag is not empty the output is: PREFIX + SEPARATOR + ORIGIN + SEPARATOR + TAG.
     * This output will be the same for notifications from the same origin that have the same input
     * tag.
     *
     * If the input tag is empty the output is PREFIX + SEPARATOR + ORIGIN + SEPARATOR +
     * NOTIFICATION_ID.
     *
     * @param notificationId The id of the notification.
     * @param origin The origin for which the notification is shown.
     * @param tag A string identifier for this notification.
     * @return The generated platform tag.
     */
@VisibleForTesting
static String makePlatformTag(String notificationId, String origin, @Nullable String tag) {
    // The given tag may contain the separator character, so add it last to make reading the
    // preceding origin token reliable. If no tag was specified (it is the default empty
    // string), make the platform tag unique by appending the notification id.
    StringBuilder builder = new StringBuilder();
    builder.append(PLATFORM_TAG_PREFIX).append(NotificationConstants.NOTIFICATION_TAG_SEPARATOR).append(origin).append(NotificationConstants.NOTIFICATION_TAG_SEPARATOR);
    if (TextUtils.isEmpty(tag)) {
        builder.append(notificationId);
    } else {
        builder.append(tag);
    }
    return builder.toString();
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 7 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class NewTabPageAdapter method finishInitialization.

/**
     * Initialises the sections to be handled by this adapter. Events about categories for which
     * a section has not been registered at this point will be ignored.
     */
@VisibleForTesting
void finishInitialization() {
    mSigninPromo.setObserver(this);
    resetSections();
    mNewTabPageManager.getSuggestionsSource().setObserver(this);
    mNewTabPageManager.registerSignInStateObserver(new SignInStateObserver() {

        @Override
        public void onSignedIn() {
            mSigninPromo.hide();
            resetSections();
        }

        @Override
        public void onSignedOut() {
            mSigninPromo.maybeShow();
        }
    });
}
Also used : SignInStateObserver(org.chromium.chrome.browser.signin.SigninManager.SignInStateObserver) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 8 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class NewTabPageAdapter method getGroupPositionOffset.

@VisibleForTesting
int getGroupPositionOffset(ItemGroup group) {
    int positionOffset = 0;
    for (ItemGroup candidateGroup : mGroups) {
        if (candidateGroup == group)
            return positionOffset;
        positionOffset += candidateGroup.getItems().size();
    }
    Log.d(TAG, "Group not found: %s", group);
    return RecyclerView.NO_POSITION;
}
Also used : SuppressLint(android.annotation.SuppressLint) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 9 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class OmahaClient method handlePostRequestIntent.

/**
     * Sends the request it is holding.
     */
@VisibleForTesting
private void handlePostRequestIntent(Intent intent) {
    if (!hasRequest()) {
        return;
    }
    // If enough time has passed since the last attempt, try sending a request.
    long currentTimestamp = getBackoffScheduler().getCurrentTime();
    if (currentTimestamp >= mTimestampForNextPostAttempt) {
        // All requests made during the same session should have the same ID.
        String sessionID = generateRandomUUID();
        boolean sendingInstallRequest = mSendInstallEvent;
        boolean succeeded = generateAndPostRequest(currentTimestamp, sessionID);
        if (succeeded && sendingInstallRequest) {
            // Only the first request ever generated should contain an install event.
            mSendInstallEvent = false;
            // Create and immediately send another request for a ping and update check.
            registerNewRequest(currentTimestamp);
            generateAndPostRequest(currentTimestamp, sessionID);
        }
    } else {
        // Set an alarm to POST at the proper time.  Previous alarms are destroyed.
        Intent postIntent = createPostRequestIntent(this);
        getBackoffScheduler().createAlarm(postIntent, mTimestampForNextPostAttempt);
    }
    // Write everything back out again to save our state.
    saveState();
}
Also used : Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 10 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class OmahaClient method restoreState.

/**
     * Reads the data back from the file it was saved to.  Uses SharedPreferences to handle I/O.
     * Sanity checks are performed on the timestamps to guard against clock changing.
     */
@VisibleForTesting
void restoreState() {
    boolean mustRewriteState = false;
    SharedPreferences preferences = getSharedPreferences(PREF_PACKAGE, Context.MODE_PRIVATE);
    Map<String, ?> items = preferences.getAll();
    // Read out the recorded data.
    long currentTime = getBackoffScheduler().getCurrentTime();
    mTimestampForNewRequest = getLongFromMap(items, PREF_TIMESTAMP_FOR_NEW_REQUEST, currentTime);
    mTimestampForNextPostAttempt = getLongFromMap(items, PREF_TIMESTAMP_FOR_NEXT_POST_ATTEMPT, currentTime);
    long requestTimestamp = getLongFromMap(items, PREF_TIMESTAMP_OF_REQUEST, INVALID_TIMESTAMP);
    // If the preference doesn't exist, it's likely that we haven't sent an install event.
    mSendInstallEvent = getBooleanFromMap(items, PREF_SEND_INSTALL_EVENT, true);
    // Restore the install source.
    String defaultInstallSource = determineInstallSource();
    mInstallSource = getStringFromMap(items, PREF_INSTALL_SOURCE, defaultInstallSource);
    // If we're not sending an install event, don't bother restoring the request ID:
    // the server does not expect to have persisted request IDs for pings or update checks.
    String persistedRequestId = mSendInstallEvent ? getStringFromMap(items, PREF_PERSISTED_REQUEST_ID, INVALID_REQUEST_ID) : INVALID_REQUEST_ID;
    mCurrentRequest = requestTimestamp == INVALID_TIMESTAMP ? null : createRequestData(requestTimestamp, persistedRequestId);
    mLatestVersion = getStringFromMap(items, PREF_LATEST_VERSION, "");
    mMarketURL = getStringFromMap(items, PREF_MARKET_URL, "");
    // If we don't have a timestamp for when we installed Chrome, then set it to now.
    mTimestampOfInstall = getLongFromMap(items, PREF_TIMESTAMP_OF_INSTALL, currentTime);
    // Confirm that the timestamp for the next request is less than the base delay.
    long delayToNewRequest = mTimestampForNewRequest - currentTime;
    if (delayToNewRequest > MS_BETWEEN_REQUESTS) {
        Log.w(TAG, "Delay to next request (" + delayToNewRequest + ") is longer than expected.  Resetting to now.");
        mTimestampForNewRequest = currentTime;
        mustRewriteState = true;
    }
    // Confirm that the timestamp for the next POST is less than the current delay.
    long delayToNextPost = mTimestampForNextPostAttempt - currentTime;
    if (delayToNextPost > getBackoffScheduler().getGeneratedDelay()) {
        Log.w(TAG, "Delay to next post attempt (" + delayToNextPost + ") is greater than expected (" + getBackoffScheduler().getGeneratedDelay() + ").  Resetting to now.");
        mTimestampForNextPostAttempt = currentTime;
        mustRewriteState = true;
    }
    if (mustRewriteState) {
        saveState();
    }
    mStateHasBeenRestored = true;
}
Also used : SharedPreferences(android.content.SharedPreferences) VisibleForTesting(org.chromium.base.VisibleForTesting)

Aggregations

VisibleForTesting (org.chromium.base.VisibleForTesting)52 Intent (android.content.Intent)6 IOException (java.io.IOException)6 SharedPreferences (android.content.SharedPreferences)5 JSONObject (org.json.JSONObject)4 PendingIntent (android.app.PendingIntent)3 SpannableString (android.text.SpannableString)3 BufferedReader (java.io.BufferedReader)3 Matcher (java.util.regex.Matcher)3 CalledByNative (org.chromium.base.annotations.CalledByNative)3 AccountManagerHelper (org.chromium.components.signin.AccountManagerHelper)3 SpanInfo (org.chromium.ui.text.SpanApplier.SpanInfo)3 SuppressLint (android.annotation.SuppressLint)2 IntentFilter (android.content.IntentFilter)2 Paint (android.graphics.Paint)2 File (java.io.File)2 FileReader (java.io.FileReader)2 HttpURLConnection (java.net.HttpURLConnection)2 ArrayList (java.util.ArrayList)2 Formatter (java.util.Formatter)2