Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.annotation.TargetApi; | |
| 8 import android.os.Build; | |
| 7 import android.view.ActionMode; | 9 import android.view.ActionMode; |
| 10 import android.view.View; | |
| 8 | 11 |
| 9 import org.chromium.base.Log; | 12 import org.chromium.base.Log; |
| 10 | 13 |
| 11 /** | 14 /** |
| 12 * An ActionMode for in-page web content selection. This class wraps an ActionMo de created | 15 * An ActionMode for in-page web content selection. This class wraps an ActionMo de created |
| 13 * by the associated View, providing modified interaction with that ActionMode. | 16 * by the associated View, providing modified interaction with that ActionMode. |
| 14 */ | 17 */ |
| 18 @TargetApi(Build.VERSION_CODES.M) | |
| 15 public class WebActionMode { | 19 public class WebActionMode { |
| 16 private static final String TAG = "cr.SelectActionMode"; | 20 private static final String TAG = "cr.WebActionMode"; |
| 17 | 21 |
| 18 protected final ActionMode mActionMode; | 22 protected final ActionMode mActionMode; |
| 23 private final View mView; | |
| 24 private boolean mHidden; | |
| 25 private boolean mPendingInvalidateContentRect; | |
| 26 | |
| 27 // Self-repeating task that repeatedly hides the ActionMode. This is | |
| 28 // required because ActionMode only exposes a temporary hide routine. | |
| 29 private final Runnable mRepeatingHideRunnable; | |
| 19 | 30 |
| 20 /** | 31 /** |
| 21 * Constructs a SelectActionMode instance wrapping a concrete ActionMode. | 32 * Constructs a SelectActionMode instance wrapping a concrete ActionMode. |
| 22 * @param actionMode the wrapped ActionMode. | 33 * @param actionMode the wrapped ActionMode. |
| 23 */ | 34 */ |
| 24 public WebActionMode(ActionMode actionMode) { | 35 public WebActionMode(ActionMode actionMode, View view) { |
| 25 assert actionMode != null; | 36 assert actionMode != null; |
| 37 assert view != null; | |
| 26 mActionMode = actionMode; | 38 mActionMode = actionMode; |
| 39 mView = view; | |
| 40 mRepeatingHideRunnable = new Runnable() { | |
| 41 @Override | |
| 42 public void run() { | |
| 43 assert mHidden; | |
| 44 mView.postDelayed(mRepeatingHideRunnable, ActionMode.DEFAULT_HID E_DURATION - 1); | |
| 45 hideTemporarily(ActionMode.DEFAULT_HIDE_DURATION); | |
| 46 } | |
| 47 }; | |
| 27 } | 48 } |
| 28 | 49 |
| 29 /** | 50 /** |
| 30 * @see ActionMode#finish() | 51 * @see ActionMode#finish() |
| 31 */ | 52 */ |
| 32 public void finish() { | 53 public void finish() { |
| 33 mActionMode.finish(); | 54 mActionMode.finish(); |
| 34 } | 55 } |
| 35 | 56 |
| 36 /** | 57 /** |
| 37 * @see ActionMode#invalidate() | 58 * @see ActionMode#invalidate() |
| 59 * Note that invalidation will also reset visibility state. The caller | |
| 60 * should account for this when making subsequent visibility updates. | |
| 38 */ | 61 */ |
| 39 public void invalidate() { | 62 public void invalidate() { |
| 63 if (mHidden) { | |
| 64 assert canHide(); | |
| 65 mHidden = false; | |
| 66 mView.removeCallbacks(mRepeatingHideRunnable); | |
| 67 mPendingInvalidateContentRect = false; | |
| 68 } | |
| 69 | |
| 40 // Try/catch necessary for framework bug, crbug.com/446717. | 70 // Try/catch necessary for framework bug, crbug.com/446717. |
| 41 try { | 71 try { |
| 42 mActionMode.invalidate(); | 72 mActionMode.invalidate(); |
| 43 } catch (NullPointerException e) { | 73 } catch (NullPointerException e) { |
| 44 Log.w(TAG, "Ignoring NPE from ActionMode.invalidate() as workaround for L", e); | 74 Log.w(TAG, "Ignoring NPE from ActionMode.invalidate() as workaround for L", e); |
| 45 } | 75 } |
| 46 } | 76 } |
| 47 | 77 |
| 48 /** | 78 /** |
| 49 * @see ActionMode#invalidateContentRect() | 79 * @see ActionMode#invalidateContentRect() |
| 50 */ | 80 */ |
| 51 public void invalidateContentRect() {} | 81 public void invalidateContentRect() { |
| 82 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| 83 if (mHidden) { | |
| 84 mPendingInvalidateContentRect = true; | |
| 85 } else { | |
| 86 mPendingInvalidateContentRect = false; | |
| 87 mActionMode.invalidateContentRect(); | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 /** | |
| 93 * @see ActionMode#onWindowFocusChanged() | |
| 94 */ | |
| 95 public void onWindowFocusChanged(boolean hasWindowFocus) { | |
| 96 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| 97 mActionMode.onWindowFocusChanged(hasWindowFocus); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * Hide or reveal the ActionMode. Note that this only has visible | |
| 103 * side-effects if the underlying ActionMode supports hiding. | |
| 104 * @param hide whether to hide or show the ActionMode. | |
| 105 */ | |
| 106 public void hide(boolean hide) { | |
| 107 if (!canHide()) return; | |
| 108 if (mHidden == hide) return; | |
| 109 mHidden = hide; | |
| 110 if (mHidden) { | |
| 111 mRepeatingHideRunnable.run(); | |
| 112 } else { | |
| 113 mHidden = false; | |
| 114 mView.removeCallbacks(mRepeatingHideRunnable); | |
| 115 // Delay the reveal slightly as there could be trailing content | |
| 116 // rect invalidations soon after showing (which would temporarily | |
| 117 // hide the ActionMode yet again). | |
| 118 hideTemporarily(300); | |
|
aurimas (slooooooooow)
2015/08/20 22:20:16
How did we pick this number? Can we pull this cons
jdduke (slow)
2015/08/20 23:13:14
It's somewhat arbitrary, 300 ms should ensure we g
| |
| 119 if (mPendingInvalidateContentRect) { | |
| 120 mPendingInvalidateContentRect = false; | |
| 121 invalidateContentRect(); | |
| 122 } | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 /** | |
| 127 * @see ActionMode#hide(long) | |
| 128 */ | |
| 129 private void hideTemporarily(long duration) { | |
| 130 assert canHide(); | |
| 131 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| 132 mActionMode.hide(duration); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 private boolean canHide() { | |
| 137 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| 138 return mActionMode.getType() == ActionMode.TYPE_FLOATING; | |
| 139 } | |
| 140 return false; | |
| 141 } | |
| 52 } | 142 } |
| OLD | NEW |