summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/delegates/BookmarkStateChangeDelegate.java
blob: a1c54d3c33cbc06be0359f81745384044e5414b8 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* -*- 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.delegates;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.AboutPages;
import org.mozilla.gecko.BrowserApp;
import org.mozilla.gecko.EditBookmarkDialog;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.GeckoSharedPrefs;
import org.mozilla.gecko.R;
import org.mozilla.gecko.SnackbarBuilder;
import org.mozilla.gecko.Tab;
import org.mozilla.gecko.Tabs;
import org.mozilla.gecko.Telemetry;
import org.mozilla.gecko.TelemetryContract;
import org.mozilla.gecko.home.HomeConfig;
import org.mozilla.gecko.promotion.SimpleHelperUI;
import org.mozilla.gecko.prompts.Prompt;
import org.mozilla.gecko.prompts.PromptListItem;
import org.mozilla.gecko.util.DrawableUtil;
import org.mozilla.gecko.util.ThreadUtils;

import java.lang.ref.WeakReference;

/**
 * Delegate to watch for bookmark state changes.
 *
 * This is responsible for showing snackbars and helper UIs related to the addition/removal
 * of bookmarks, or reader view bookmarks.
 */
public class BookmarkStateChangeDelegate extends BrowserAppDelegateWithReference implements Tabs.OnTabsChangedListener {
    private static final String LOGTAG = "BookmarkDelegate";

    @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 BOOKMARK_ADDED:
                // We always show the special offline snackbar whenever we bookmark a reader page.
                // It's possible that the page is already stored offline, however this is highly
                // unlikely, and even so it is probably nicer to show the same offline notification
                // every time we bookmark an about:reader page.
                if (!AboutPages.isAboutReader(tab.getURL())) {
                    showBookmarkAddedSnackbar();
                } else {
                    if (!promoteReaderViewBookmarkAdded()) {
                        showReaderModeBookmarkAddedSnackbar();
                    }
                }
                break;

            case BOOKMARK_REMOVED:
                showBookmarkRemovedSnackbar();
                break;
        }
    }

    @Override
    public void onActivityResult(BrowserApp browserApp, int requestCode, int resultCode, Intent data) {
        if (requestCode == BrowserApp.ACTIVITY_REQUEST_FIRST_READERVIEW_BOOKMARK) {
            if (resultCode == BrowserApp.ACTIVITY_RESULT_FIRST_READERVIEW_BOOKMARKS_GOTO_BOOKMARKS) {
                browserApp.openUrlAndStopEditing("about:home?panel=" + HomeConfig.getIdForBuiltinPanelType(HomeConfig.PanelType.BOOKMARKS));
            } else if (resultCode == BrowserApp.ACTIVITY_RESULT_FIRST_READERVIEW_BOOKMARKS_IGNORE) {
                showReaderModeBookmarkAddedSnackbar();
            }
        }
    }

    private boolean promoteReaderViewBookmarkAdded() {
        final BrowserApp browserApp = getBrowserApp();
        if (browserApp == null) {
            return false;
        }

        final SharedPreferences prefs = GeckoSharedPrefs.forProfile(browserApp);

        final boolean hasFirstReaderViewPromptBeenShownBefore = prefs.getBoolean(SimpleHelperUI.PREF_FIRST_RVBP_SHOWN, false);

        if (hasFirstReaderViewPromptBeenShownBefore) {
            return false;
        }

        SimpleHelperUI.show(browserApp,
                SimpleHelperUI.FIRST_RVBP_SHOWN_TELEMETRYEXTRA,
                BrowserApp.ACTIVITY_REQUEST_FIRST_READERVIEW_BOOKMARK,
                R.string.helper_first_offline_bookmark_title, R.string.helper_first_offline_bookmark_message,
                R.drawable.helper_readerview_bookmark, R.string.helper_first_offline_bookmark_button,
                BrowserApp.ACTIVITY_RESULT_FIRST_READERVIEW_BOOKMARKS_GOTO_BOOKMARKS,
                BrowserApp.ACTIVITY_RESULT_FIRST_READERVIEW_BOOKMARKS_IGNORE);

        GeckoSharedPrefs.forProfile(browserApp)
                .edit()
                .putBoolean(SimpleHelperUI.PREF_FIRST_RVBP_SHOWN, true)
                .apply();

        return true;
    }

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

        // This flow is from the option menu which has check to see if a bookmark was already added.
        // So, it is safe here to show the snackbar that bookmark_added without any checks.
        final SnackbarBuilder.SnackbarCallback callback = new SnackbarBuilder.SnackbarCallback() {
            @Override
            public void onClick(View v) {
                Telemetry.sendUIEvent(TelemetryContract.Event.SHOW, TelemetryContract.Method.TOAST, "bookmark_options");
                showBookmarkDialog(browserApp);
            }
        };

        SnackbarBuilder.builder(browserApp)
                .message(R.string.bookmark_added)
                .duration(Snackbar.LENGTH_LONG)
                .action(R.string.bookmark_options)
                .callback(callback)
                .buildAndShow();
    }

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

        SnackbarBuilder.builder(browserApp)
                .message(R.string.bookmark_removed)
                .duration(Snackbar.LENGTH_LONG)
                .buildAndShow();
    }

    private static void showBookmarkDialog(final BrowserApp browserApp) {
        final Resources res = browserApp.getResources();
        final Tab tab = Tabs.getInstance().getSelectedTab();

        final Prompt ps = new Prompt(browserApp, new Prompt.PromptCallback() {
            @Override
            public void onPromptFinished(String result) {
                int itemId = -1;
                try {
                    itemId = new JSONObject(result).getInt("button");
                } catch (JSONException ex) {
                    Log.e(LOGTAG, "Exception reading bookmark prompt result", ex);
                }

                if (tab == null) {
                    return;
                }

                if (itemId == 0) {
                    final String extrasId = res.getResourceEntryName(R.string.contextmenu_edit_bookmark);
                    Telemetry.sendUIEvent(TelemetryContract.Event.ACTION,
                            TelemetryContract.Method.DIALOG, extrasId);

                    new EditBookmarkDialog(browserApp).show(tab.getURL());
                } else if (itemId == 1) {
                    final String extrasId = res.getResourceEntryName(R.string.contextmenu_add_to_launcher);
                    Telemetry.sendUIEvent(TelemetryContract.Event.ACTION,
                            TelemetryContract.Method.DIALOG, extrasId);

                    final String url = tab.getURL();
                    final String title = tab.getDisplayTitle();

                    if (url != null && title != null) {
                        ThreadUtils.postToBackgroundThread(new Runnable() {
                            @Override
                            public void run() {
                                GeckoAppShell.createShortcut(title, url);
                            }
                        });
                    }
                }
            }
        });

        final PromptListItem[] items = new PromptListItem[2];
        items[0] = new PromptListItem(res.getString(R.string.contextmenu_edit_bookmark));
        items[1] = new PromptListItem(res.getString(R.string.contextmenu_add_to_launcher));

        ps.show("", "", items, ListView.CHOICE_MODE_NONE);
    }

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

        final Drawable iconDownloaded = DrawableUtil.tintDrawable(browserApp, R.drawable.status_icon_readercache, Color.WHITE);

        final SnackbarBuilder.SnackbarCallback callback = new SnackbarBuilder.SnackbarCallback() {
            @Override
            public void onClick(View v) {
                browserApp.openUrlAndStopEditing("about:home?panel=" + HomeConfig.getIdForBuiltinPanelType(HomeConfig.PanelType.BOOKMARKS));
            }
        };

        SnackbarBuilder.builder(browserApp)
                .message(R.string.reader_saved_offline)
                .duration(Snackbar.LENGTH_LONG)
                .action(R.string.reader_switch_to_bookmarks)
                .callback(callback)
                .icon(iconDownloaded)
                .backgroundColor(ContextCompat.getColor(browserApp, R.color.link_blue))
                .actionColor(Color.WHITE)
                .buildAndShow();
    }
}