Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.browseractions; | 5 package org.chromium.chrome.browser.browseractions; |
| 6 | 6 |
| 7 import android.app.PendingIntent; | 7 import android.app.PendingIntent; |
| 8 import android.app.ProgressDialog; | |
| 8 import android.support.customtabs.browseractions.BrowserActionItem; | 9 import android.support.customtabs.browseractions.BrowserActionItem; |
| 9 import android.support.customtabs.browseractions.BrowserActionsIntent; | 10 import android.support.customtabs.browseractions.BrowserActionsIntent; |
| 10 import android.util.Pair; | 11 import android.util.Pair; |
| 11 import android.util.SparseArray; | 12 import android.util.SparseArray; |
| 12 import android.view.ContextMenu; | 13 import android.view.ContextMenu; |
| 13 import android.view.ContextMenu.ContextMenuInfo; | 14 import android.view.ContextMenu.ContextMenuInfo; |
| 14 import android.view.View; | 15 import android.view.View; |
| 15 import android.view.View.OnAttachStateChangeListener; | 16 import android.view.View.OnAttachStateChangeListener; |
| 16 import android.view.View.OnCreateContextMenuListener; | 17 import android.view.View.OnCreateContextMenuListener; |
| 17 | 18 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 private final ContextMenuParams mCurrentContextMenuParams; | 53 private final ContextMenuParams mCurrentContextMenuParams; |
| 53 private final BrowserActionsContextMenuItemDelegate mDelegate; | 54 private final BrowserActionsContextMenuItemDelegate mDelegate; |
| 54 private final BrowserActionActivity mActivity; | 55 private final BrowserActionActivity mActivity; |
| 55 private final Callback<Integer> mItemSelectedCallback; | 56 private final Callback<Integer> mItemSelectedCallback; |
| 56 private final Runnable mOnMenuShown; | 57 private final Runnable mOnMenuShown; |
| 57 private final Runnable mOnMenuClosed; | 58 private final Runnable mOnMenuClosed; |
| 58 private final Callback<Boolean> mOnShareClickedRunnable; | 59 private final Callback<Boolean> mOnShareClickedRunnable; |
| 59 | 60 |
| 60 private final List<Pair<Integer, List<ContextMenuItem>>> mItems; | 61 private final List<Pair<Integer, List<ContextMenuItem>>> mItems; |
| 61 | 62 |
| 63 private final ProgressDialog mProgressDialog; | |
| 64 | |
| 65 private boolean mIsOpenInBackgroundPending; | |
| 66 | |
| 62 public BrowserActionsContextMenuHelper(BrowserActionActivity activity, Conte xtMenuParams params, | 67 public BrowserActionsContextMenuHelper(BrowserActionActivity activity, Conte xtMenuParams params, |
|
Yusuf
2017/07/10 06:14:56
Looking at how we are using the Activity here, if
ltian
2017/07/26 01:22:12
Done.
| |
| 63 List<BrowserActionItem> customItems, String sourcePackageName) { | 68 List<BrowserActionItem> customItems, String sourcePackageName) { |
| 64 mActivity = activity; | 69 mActivity = activity; |
| 65 mCurrentContextMenuParams = params; | 70 mCurrentContextMenuParams = params; |
| 66 mOnMenuShown = new Runnable() { | 71 mOnMenuShown = new Runnable() { |
| 67 @Override | 72 @Override |
| 68 public void run() { | 73 public void run() { |
| 69 mActivity.onMenuShown(); | 74 mActivity.onMenuShown(); |
| 70 } | 75 } |
| 71 }; | 76 }; |
| 72 mOnMenuClosed = new Runnable() { | 77 mOnMenuClosed = new Runnable() { |
| 73 @Override | 78 @Override |
| 74 public void run() { | 79 public void run() { |
| 75 mActivity.finish(); | 80 if (!mIsOpenInBackgroundPending) { |
| 81 mActivity.finish(); | |
| 82 } | |
| 76 } | 83 } |
| 77 }; | 84 }; |
| 78 mItemSelectedCallback = new Callback<Integer>() { | 85 mItemSelectedCallback = new Callback<Integer>() { |
| 79 @Override | 86 @Override |
| 80 public void onResult(Integer result) { | 87 public void onResult(Integer result) { |
| 81 onItemSelected(result); | 88 onItemSelected(result); |
| 82 } | 89 } |
| 83 }; | 90 }; |
| 84 mOnShareClickedRunnable = new Callback<Boolean>() { | 91 mOnShareClickedRunnable = new Callback<Boolean>() { |
| 85 @Override | 92 @Override |
| 86 public void onResult(Boolean isShareLink) { | 93 public void onResult(Boolean isShareLink) { |
| 87 mDelegate.share(true, mCurrentContextMenuParams.getLinkUrl()); | 94 mDelegate.share(true, mCurrentContextMenuParams.getLinkUrl()); |
| 88 } | 95 } |
| 89 }; | 96 }; |
| 90 ShareContextMenuItem shareItem = new ShareContextMenuItem(R.drawable.ic_ share_white_24dp, | 97 ShareContextMenuItem shareItem = new ShareContextMenuItem(R.drawable.ic_ share_white_24dp, |
| 91 R.string.browser_actions_share, R.id.browser_actions_share, true ); | 98 R.string.browser_actions_share, R.id.browser_actions_share, true ); |
| 92 shareItem.setCreatorPackageName(sourcePackageName); | 99 shareItem.setCreatorPackageName(sourcePackageName); |
| 93 mBrowserActionsLinkGroup = | 100 mBrowserActionsLinkGroup = |
| 94 Arrays.asList(ChromeContextMenuItem.BROWSER_ACTIONS_OPEN_IN_BACK GROUND, | 101 Arrays.asList(ChromeContextMenuItem.BROWSER_ACTIONS_OPEN_IN_BACK GROUND, |
| 95 ChromeContextMenuItem.BROWSER_ACTIONS_OPEN_IN_INCOGNITO_ TAB, | 102 ChromeContextMenuItem.BROWSER_ACTIONS_OPEN_IN_INCOGNITO_ TAB, |
| 96 ChromeContextMenuItem.BROWSER_ACTION_SAVE_LINK_AS, | 103 ChromeContextMenuItem.BROWSER_ACTION_SAVE_LINK_AS, |
| 97 ChromeContextMenuItem.BROWSER_ACTIONS_COPY_ADDRESS, shar eItem); | 104 ChromeContextMenuItem.BROWSER_ACTIONS_COPY_ADDRESS, shar eItem); |
| 98 mDelegate = new BrowserActionsContextMenuItemDelegate(mActivity, sourceP ackageName); | 105 mDelegate = new BrowserActionsContextMenuItemDelegate(mActivity, sourceP ackageName); |
| 106 mProgressDialog = new ProgressDialog(mActivity); | |
| 99 | 107 |
| 100 mItems = buildContextMenuItems(customItems); | 108 mItems = buildContextMenuItems(customItems); |
| 101 } | 109 } |
| 102 | 110 |
| 103 /** | 111 /** |
| 104 * Builds items for Browser Actions context menu. | 112 * Builds items for Browser Actions context menu. |
| 105 */ | 113 */ |
| 106 private List<Pair<Integer, List<ContextMenuItem>>> buildContextMenuItems( | 114 private List<Pair<Integer, List<ContextMenuItem>>> buildContextMenuItems( |
| 107 List<BrowserActionItem> customItems) { | 115 List<BrowserActionItem> customItems) { |
| 108 List<Pair<Integer, List<ContextMenuItem>>> menuItems = new ArrayList<>() ; | 116 List<Pair<Integer, List<ContextMenuItem>>> menuItems = new ArrayList<>() ; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 124 for (int i = 0; i < customItems.size() && i < BrowserActionsIntent.MAX_C USTOM_ITEMS; i++) { | 132 for (int i = 0; i < customItems.size() && i < BrowserActionsIntent.MAX_C USTOM_ITEMS; i++) { |
| 125 items.add(new BrowserActionsCustomContextMenuItem( | 133 items.add(new BrowserActionsCustomContextMenuItem( |
| 126 CUSTOM_BROWSER_ACTIONS_ID_GROUP.get(i), customItems.get(i))) ; | 134 CUSTOM_BROWSER_ACTIONS_ID_GROUP.get(i), customItems.get(i))) ; |
| 127 mCustomItemActionMap.put( | 135 mCustomItemActionMap.put( |
| 128 CUSTOM_BROWSER_ACTIONS_ID_GROUP.get(i), customItems.get(i).g etAction()); | 136 CUSTOM_BROWSER_ACTIONS_ID_GROUP.get(i), customItems.get(i).g etAction()); |
| 129 } | 137 } |
| 130 } | 138 } |
| 131 | 139 |
| 132 private boolean onItemSelected(int itemId) { | 140 private boolean onItemSelected(int itemId) { |
| 133 if (itemId == R.id.browser_actions_open_in_background) { | 141 if (itemId == R.id.browser_actions_open_in_background) { |
| 134 mDelegate.onOpenInBackground(mCurrentContextMenuParams.getLinkUrl()) ; | 142 if (mActivity.isNativeInitialized()) { |
| 143 mDelegate.onOpenInBackground(mCurrentContextMenuParams.getLinkUr l()); | |
| 144 } else { | |
| 145 mIsOpenInBackgroundPending = true; | |
| 146 waitNativeInitialized(); | |
| 147 } | |
| 135 } else if (itemId == R.id.browser_actions_open_in_incognito_tab) { | 148 } else if (itemId == R.id.browser_actions_open_in_incognito_tab) { |
| 136 mDelegate.onOpenInIncognitoTab(mCurrentContextMenuParams.getLinkUrl( )); | 149 mDelegate.onOpenInIncognitoTab(mCurrentContextMenuParams.getLinkUrl( )); |
| 137 } else if (itemId == R.id.browser_actions_save_link_as) { | 150 } else if (itemId == R.id.browser_actions_save_link_as) { |
| 138 mDelegate.startDownload(mCurrentContextMenuParams.getLinkUrl()); | 151 mDelegate.startDownload(mCurrentContextMenuParams.getLinkUrl()); |
| 139 } else if (itemId == R.id.browser_actions_copy_address) { | 152 } else if (itemId == R.id.browser_actions_copy_address) { |
| 140 mDelegate.onSaveToClipboard(mCurrentContextMenuParams.getLinkUrl()); | 153 mDelegate.onSaveToClipboard(mCurrentContextMenuParams.getLinkUrl()); |
| 141 } else if (itemId == R.id.browser_actions_share) { | 154 } else if (itemId == R.id.browser_actions_share) { |
| 142 mDelegate.share(false, mCurrentContextMenuParams.getLinkUrl()); | 155 mDelegate.share(false, mCurrentContextMenuParams.getLinkUrl()); |
| 143 } else if (mCustomItemActionMap.indexOfKey(itemId) >= 0) { | 156 } else if (mCustomItemActionMap.indexOfKey(itemId) >= 0) { |
| 144 mDelegate.onCustomItemSelected(mCustomItemActionMap.get(itemId)); | 157 mDelegate.onCustomItemSelected(mCustomItemActionMap.get(itemId)); |
| 145 } | 158 } |
| 146 return true; | 159 return true; |
| 147 } | 160 } |
| 148 | 161 |
| 149 /** | 162 /** |
| 163 * Display a progress dialog to wait for native libraries initialized. | |
| 164 */ | |
| 165 private void waitNativeInitialized() { | |
| 166 mProgressDialog.setMessage( | |
| 167 mActivity.getString(R.string.browser_actions_loading_native_mess age)); | |
| 168 mProgressDialog.show(); | |
| 169 } | |
| 170 | |
| 171 private void dismissProgressDialog() { | |
| 172 if (mProgressDialog != null && mProgressDialog.isShowing()) { | |
| 173 mProgressDialog.dismiss(); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 /** | |
| 150 * Displays the Browser Actions context menu. | 178 * Displays the Browser Actions context menu. |
| 151 * @param view The view to show the context menu if old UI is used. | 179 * @param view The view to show the context menu if old UI is used. |
| 152 */ | 180 */ |
| 153 public void displayBrowserActionsMenu(final View view) { | 181 public void displayBrowserActionsMenu(final View view) { |
| 154 if (IS_NEW_UI_ENABLED) { | 182 if (IS_NEW_UI_ENABLED) { |
| 155 ContextMenuUi menuUi = new TabularContextMenuUi(mOnShareClickedRunna ble); | 183 ContextMenuUi menuUi = new TabularContextMenuUi(mOnShareClickedRunna ble); |
| 156 menuUi.displayMenu(mActivity, mCurrentContextMenuParams, mItems, mIt emSelectedCallback, | 184 menuUi.displayMenu(mActivity, mCurrentContextMenuParams, mItems, mIt emSelectedCallback, |
| 157 mOnMenuShown, mOnMenuClosed); | 185 mOnMenuShown, mOnMenuClosed); |
| 158 } else { | 186 } else { |
| 159 view.setOnCreateContextMenuListener(BrowserActionsContextMenuHelper. this); | 187 view.setOnCreateContextMenuListener(BrowserActionsContextMenuHelper. this); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 176 | 204 |
| 177 @Override | 205 @Override |
| 178 public void onViewAttachedToWindow(View view) { | 206 public void onViewAttachedToWindow(View view) { |
| 179 if (view.showContextMenu()) { | 207 if (view.showContextMenu()) { |
| 180 mOnMenuShown.run(); | 208 mOnMenuShown.run(); |
| 181 } | 209 } |
| 182 } | 210 } |
| 183 | 211 |
| 184 @Override | 212 @Override |
| 185 public void onViewDetachedFromWindow(View v) {} | 213 public void onViewDetachedFromWindow(View v) {} |
| 214 | |
| 215 /** | |
| 216 * Finishes all pending actions which requires Chrome native libraries. | |
| 217 */ | |
| 218 public void handlePendingActions() { | |
|
Yusuf
2017/07/10 06:14:56
onNativeInitialized
ltian
2017/07/26 01:22:12
Done.
| |
| 219 if (mIsOpenInBackgroundPending) { | |
| 220 mIsOpenInBackgroundPending = false; | |
| 221 dismissProgressDialog(); | |
| 222 mDelegate.onOpenInBackground(mCurrentContextMenuParams.getLinkUrl()) ; | |
| 223 mActivity.finish(); | |
| 224 } | |
| 225 } | |
| 186 } | 226 } |
| OLD | NEW |