summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/promotion/ReaderViewBookmarkPromotion.java
blob: db5a531c665aaaec572cc5fef30466b0b134b004 (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
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.promotion;

import android.content.Intent;
import android.content.SharedPreferences;

import com.keepsafe.switchboard.SwitchBoard;

import org.mozilla.gecko.BrowserApp;
import org.mozilla.gecko.GeckoSharedPrefs;
import org.mozilla.gecko.R;
import org.mozilla.gecko.Tab;
import org.mozilla.gecko.Tabs;
import org.mozilla.gecko.delegates.BrowserAppDelegateWithReference;
import org.mozilla.gecko.reader.ReaderModeUtils;
import org.mozilla.gecko.Experiments;

public class ReaderViewBookmarkPromotion extends BrowserAppDelegateWithReference implements Tabs.OnTabsChangedListener {
    private static final String PREF_FIRST_RV_HINT_SHOWN = "first_reader_view_hint_shown";
    private static final String FIRST_READERVIEW_OPEN_TELEMETRYEXTRA = "first_readerview_open_prompt";

    private boolean hasEnteredReaderMode = false;

    @Override
    public void onResume(BrowserApp browserApp) {
        Tabs.registerOnTabsChangedListener(this);
    }

    @Override
    public void onPause(BrowserApp browserApp) {
        Tabs.unregisterOnTabsChangedListener(this);
    }

    @Override
    public void onTabChanged(Tab tab, Tabs.TabEvents msg, String data) {
        switch (msg) {
            case LOCATION_CHANGE:
                // old url: data
                // new url: tab.getURL()
                final boolean enteringReaderMode = ReaderModeUtils.isEnteringReaderMode(data, tab.getURL());

                if (!hasEnteredReaderMode && enteringReaderMode) {
                    hasEnteredReaderMode = true;
                    promoteBookmarking();
                }

                break;
        }
    }

    @Override
    public void onActivityResult(BrowserApp browserApp, int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case BrowserApp.ACTIVITY_REQUEST_TRIPLE_READERVIEW:
                if (resultCode == BrowserApp.ACTIVITY_RESULT_TRIPLE_READERVIEW_ADD_BOOKMARK) {
                    final Tab tab = Tabs.getInstance().getSelectedTab();
                    if (tab != null) {
                        tab.addBookmark();
                    }
                } else if (resultCode == BrowserApp.ACTIVITY_RESULT_TRIPLE_READERVIEW_IGNORE) {
                    // Nothing to do: we won't show this promotion again either way.
                }
                break;
        }
    }

    private void promoteBookmarking() {
        final BrowserApp browserApp = getBrowserApp();
        if (browserApp == null) {
            return;
        }

        final SharedPreferences prefs = GeckoSharedPrefs.forProfile(browserApp);
        final boolean isEnabled = SwitchBoard.isInExperiment(browserApp, Experiments.TRIPLE_READERVIEW_BOOKMARK_PROMPT);

        // We reuse the same preference as for the first offline reader view bookmark
        // as we only want to show one of the two UIs (they both explain the same
        // functionality).
        if (!isEnabled || prefs.getBoolean(PREF_FIRST_RV_HINT_SHOWN, false)) {
            return;
        }

        SimpleHelperUI.show(browserApp,
                FIRST_READERVIEW_OPEN_TELEMETRYEXTRA,
                BrowserApp.ACTIVITY_REQUEST_TRIPLE_READERVIEW,
                R.string.helper_triple_readerview_open_title,
                R.string.helper_triple_readerview_open_message,
                R.drawable.helper_readerview_bookmark, // We share the icon with the usual helper UI
                R.string.helper_triple_readerview_open_button,
                BrowserApp.ACTIVITY_RESULT_TRIPLE_READERVIEW_ADD_BOOKMARK,
                BrowserApp.ACTIVITY_RESULT_TRIPLE_READERVIEW_IGNORE);

        prefs
                .edit()
                .putBoolean(PREF_FIRST_RV_HINT_SHOWN, true)
                .apply();
    }

}