Bug 1377286 - Allow pinning items to Activity Stream from other home panels r=sebastian

Menu item to pin/unpin items from Bookmarks and History panels is only displayed when
Activity Stream is enabled.

MozReview-Commit-ID: Ko3xmpF2R53

--HG--
extra : rebase_source : 4eefb81224c1cc00ba2eb6147e26119af647d317
This commit is contained in:
Grigory Kruglov 2017-07-12 17:58:45 -04:00
Родитель 8aeab5083f
Коммит 285d2e60bb
5 изменённых файлов: 113 добавлений и 1 удалений

Просмотреть файл

@ -32,6 +32,9 @@
<item android:id="@+id/home_remove"
android:title="@string/contextmenu_remove"/>
<item android:id="@+id/home_as_pin"
android:title="@string/contextmenu_top_sites_pin"/>
<item android:id="@+id/home_add_to_launcher"
android:title="@string/contextmenu_add_to_launcher"/>

Просмотреть файл

@ -9,6 +9,7 @@ import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.util.StringUtils;
import android.database.Cursor;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
@ -27,6 +28,8 @@ public class HomeContextMenuInfo extends AdapterContextMenuInfo {
public int bookmarkId = -1;
public RemoveItemType itemType = null;
/* package-private */ @Nullable Boolean isAsPinned;
// Item type to be handled with "Remove" selection.
/* package-private */ enum RemoveItemType {
BOOKMARKS, COMBINED, HISTORY
@ -55,6 +58,10 @@ public class HomeContextMenuInfo extends AdapterContextMenuInfo {
return hasBookmarkId() || hasHistoryId() || hasPartnerBookmarkId();
}
/* package-private */ void updateAsPinned(boolean pinnedState) {
isAsPinned = pinnedState;
}
private boolean hasHistoryId() {
return historyId > -1;
}

Просмотреть файл

@ -16,6 +16,7 @@ import org.mozilla.gecko.R;
import org.mozilla.gecko.SnackbarBuilder;
import org.mozilla.gecko.Telemetry;
import org.mozilla.gecko.TelemetryContract;
import org.mozilla.gecko.activitystream.ActivityStream;
import org.mozilla.gecko.bookmarks.BookmarkUtils;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserContract.SuggestedSites;
@ -162,7 +163,7 @@ public abstract class HomeFragment extends Fragment {
return;
}
HomeContextMenuInfo info = (HomeContextMenuInfo) menuInfo;
final HomeContextMenuInfo info = (HomeContextMenuInfo) menuInfo;
// Don't show the context menu for folders if full bookmark management isn't enabled.
final boolean enableFullBookmarkManagement = BookmarkUtils.isEnabled(getContext());
@ -209,7 +210,41 @@ public abstract class HomeFragment extends Fragment {
menu.findItem(R.id.home_share).setVisible(false);
menu.findItem(R.id.home_add_to_launcher).setVisible(false);
menu.findItem(R.id.home_set_as_homepage).setVisible(false);
menu.findItem(R.id.home_as_pin).setVisible(false);
return;
}
// If Activity Stream is disabled, simply hide "AS Pin" menu item as classic Top Sites do not
// support pinning from outside of the Top Site tiles.
if (!ActivityStream.isEnabled(getContext())) {
menu.findItem(R.id.home_as_pin).setVisible(false);
return;
}
// Asynchronously update pin state for this item.
// This code should be superseded by context menu unification work in Bug 1377292.
final MenuItem asPinItem = menu.findItem(R.id.home_as_pin);
// Do not let user interact with this menu item before we figure out pinned state.
asPinItem.setEnabled(false);
(new UIAsyncTask.WithoutParams<Boolean>(ThreadUtils.getBackgroundHandler()) {
@Override
protected Boolean doInBackground() {
return BrowserDB.from(getContext()).isPinnedForAS(
getContext().getContentResolver(), info.url);
}
@Override
protected void onPostExecute(Boolean hasPin) {
if (hasPin) {
asPinItem.setTitle(R.string.contextmenu_top_sites_unpin);
}
info.updateAsPinned(hasPin);
asPinItem.setEnabled(true);
}
}).execute();
}
@Override
@ -327,6 +362,12 @@ public abstract class HomeFragment extends Fragment {
getResources().getResourceEntryName(itemId));
return true;
}
if (itemId == R.id.home_as_pin) {
new ToggleASPinTask(getActivity(), info).execute();
return true;
}
return false;
}
@ -399,6 +440,59 @@ public abstract class HomeFragment extends Fragment {
mIsLoaded = true;
}
private static class ToggleASPinTask extends UIAsyncTask.WithoutParams<Void> {
private final WeakReference<Activity> activityWeakReference;
private final Context context;
private final HomeContextMenuInfo info;
private final BrowserDB db;
private final ContentResolver cr;
private final boolean toggleWillPin;
ToggleASPinTask(Activity activity, HomeContextMenuInfo info) {
super(ThreadUtils.getBackgroundHandler());
this.activityWeakReference = new WeakReference<Activity>(activity);
this.context = activity.getApplicationContext();
this.db = BrowserDB.from(context);
this.info = info;
this.cr = context.getContentResolver();
if (info.isAsPinned == null) {
throw new IllegalStateException("Tried changing pinned state before it's determined.");
}
this.toggleWillPin = !info.isAsPinned;
}
@Override
protected Void doInBackground() {
if (toggleWillPin) {
db.pinSiteForAS(cr, info.url, info.title);
} else {
db.unpinSiteForAS(cr, info.url);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
final Activity activity = activityWeakReference.get();
if (activity == null || activity.isFinishing()) {
return;
}
int messageId = R.string.home_pinned_site;
if (!toggleWillPin) {
messageId = R.string.home_unpinned_site;
}
SnackbarBuilder.builder(activity)
.message(messageId)
.duration(Snackbar.LENGTH_LONG)
.buildAndShow();
}
}
static class RemoveItemTask extends UIAsyncTask.WithoutParams<Void> {
private final WeakReference<Activity> activityWeakReference;
private final Context context;

Просмотреть файл

@ -625,6 +625,11 @@
the text of a button; we expect more than one device. -->
<!ENTITY home_remote_tabs_unhide_selected_devices "Unhide selected devices">
<!-- Localization note (home_pinned_site) : This is a snackbar label displayed after
a site is pinned or unpinned. -->
<!ENTITY home_pinned_site "Pinned site">
<!ENTITY home_unpinned_site "Unpinned site">
<!ENTITY remote_tabs_panel_moved_title "Where did my tabs go?">
<!ENTITY remote_tabs_panel_moved_desc "We\'ve moved your tabs from other devices into a panel on your home page that can be easily accessed every time you open a new tab.">
<!ENTITY remote_tabs_panel_moved_link "Take me to my new panel.">

Просмотреть файл

@ -481,6 +481,9 @@
<string name="home_remote_tabs_unhide_selected_devices">&home_remote_tabs_unhide_selected_devices;</string>
<string name="pin_site_dialog_hint">&pin_site_dialog_hint;</string>
<string name="home_pinned_site">&home_pinned_site;</string>
<string name="home_unpinned_site">&home_unpinned_site;</string>
<string name="remote_tabs_never_synced">&remote_tabs_never_synced;</string>
<string name="filepicker_title">&filepicker_title;</string>