summaryrefslogtreecommitdiffstats
path: root/chrome/android/java/src/org/chromium/chrome/browser/UpgradeActivity.java
blob: e203e67c9ed33799caa51c1172f5e495906b6760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;

import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.ApplicationStatus;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.tabmodel.DocumentModeAssassin;
import org.chromium.chrome.browser.tabmodel.DocumentModeAssassin.DocumentModeAssassinObserver;
import org.chromium.chrome.browser.util.IntentUtils;

/**
 * Activity that interrupts launch and shows that users are being upgraded to a new version of
 * Chrome.
 *
 * TODO(dfalcantara): Do we need to worry about onNewIntent()?
 */
public class UpgradeActivity extends AppCompatActivity {
    public static final String EXTRA_INTENT_TO_REFIRE =
            "org.chromium.chrome.browser.INTENT_TO_REFIRE";

    private static final long MIN_MS_TO_DISPLAY_ACTIVITY = 500;
    private static final long INVALID_TIMESTAMP = -1;

    private final Handler mHandler;
    private final DocumentModeAssassinObserver mObserver;

    private Intent mIntentToFireAfterUpgrade;
    private long mStartTimestamp = INVALID_TIMESTAMP;
    private boolean mIsDestroyed;

    public static void launchInstance(Context context, Intent originalIntent) {
        Intent intent = new Intent();
        intent.setClass(context, UpgradeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(UpgradeActivity.EXTRA_INTENT_TO_REFIRE, originalIntent);
        context.startActivity(intent);
    }

    public UpgradeActivity() {
        mHandler = new Handler(Looper.getMainLooper());

        mObserver = new DocumentModeAssassinObserver() {
            @Override
            public void onStageChange(int newStage) {
                if (newStage != DocumentModeAssassin.STAGE_DONE) return;
                DocumentModeAssassin.getInstance().removeObserver(this);

                // Always post to avoid any issues that could arise from firing the Runnable
                // while other Observers are being alerted.
                long msElapsed = System.currentTimeMillis() - mStartTimestamp;
                long msRemaining = Math.max(0, MIN_MS_TO_DISPLAY_ACTIVITY - msElapsed);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        continueApplicationLaunch();
                    }
                }, msRemaining);
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIntentToFireAfterUpgrade = getIntentToFireAfterUpgrade(getIntent());
        setContentView(R.layout.upgrade_activity);

        DocumentModeAssassin assassin = DocumentModeAssassin.getInstance();
        if (!DocumentModeAssassin.isMigrationNecessary()
                || assassin.getStage() == DocumentModeAssassin.STAGE_DONE) {
            // Migration finished in the background.
            continueApplicationLaunch();
        } else {
            // Kick off migration if it hasn't already started.
            assassin.addObserver(mObserver);
            assassin.migrateFromDocumentToTabbedMode();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Set the timestamp after the Activity is visible to avoid shortening the timer.
        if (mStartTimestamp == INVALID_TIMESTAMP) mStartTimestamp = System.currentTimeMillis();
    }

    @Override
    protected void onDestroy() {
        mIsDestroyed = true;
        super.onDestroy();
    }

    private static Intent getIntentToFireAfterUpgrade(Intent activityIntent) {
        Intent intentToFire = null;

        // Retrieve the Intent that caused the user to end up on the upgrade pathway.
        if (activityIntent != null) {
            intentToFire = (Intent) IntentUtils.safeGetParcelableExtra(
                    activityIntent, EXTRA_INTENT_TO_REFIRE);
        }

        // If there's no Intent to refire, send them to the browser.
        if (intentToFire == null) {
            intentToFire = new Intent(Intent.ACTION_MAIN);
            intentToFire.setPackage(ApplicationStatus.getApplicationContext().getPackageName());
        }

        // Fire the Intent into a different task so that this one can go away.
        intentToFire.addFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT);

        return intentToFire;
    }

    private void continueApplicationLaunch() {
        if (mIsDestroyed) return;

        ApiCompatibilityUtils.finishAndRemoveTask(this);
        if (mIntentToFireAfterUpgrade != null && ApplicationStatus.hasVisibleActivities()) {
            startActivity(mIntentToFireAfterUpgrade);
            overridePendingTransition(android.R.anim.fade_in, 0);
            mIntentToFireAfterUpgrade = null;
        }
    }
}