зеркало из https://github.com/mozilla/gecko-dev.git
Bug 850318 - Avoid blocking the UI thread on the background thread when calling getClipboardText. r=wesj
This commit is contained in:
Родитель
30c94d7c71
Коммит
1dbc6eb9d5
|
@ -1110,41 +1110,51 @@ public class GeckoAppShell
|
|||
return intent;
|
||||
}
|
||||
|
||||
static SynchronousQueue<String> sClipboardQueue =
|
||||
new SynchronousQueue<String>();
|
||||
private static String EMPTY_STRING = new String();
|
||||
|
||||
// On some devices, access to the clipboard service needs to happen
|
||||
// on a thread with a looper, so dispatch this to our looper thread
|
||||
// Note: the main looper won't work because it may be blocked on the
|
||||
// gecko thread, which is most likely this thread
|
||||
static String getClipboardText() {
|
||||
ThreadUtils.postToBackgroundThread(new Runnable() {
|
||||
@Override
|
||||
/* On some devices, access to the clipboard service needs to happen
|
||||
* on a thread with a looper, so this function requires a looper is
|
||||
* present on the thread. */
|
||||
@SuppressWarnings("deprecation")
|
||||
public void run() {
|
||||
private static String getClipboardTextImpl() {
|
||||
Context context = GeckoApp.mAppContext;
|
||||
String text = null;
|
||||
if (android.os.Build.VERSION.SDK_INT >= 11) {
|
||||
android.content.ClipboardManager cm = (android.content.ClipboardManager)
|
||||
context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
android.content.ClipboardManager cm = (android.content.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
if (cm.hasPrimaryClip()) {
|
||||
ClipData clip = cm.getPrimaryClip();
|
||||
if (clip != null) {
|
||||
ClipData.Item item = clip.getItemAt(0);
|
||||
text = item.coerceToText(context).toString();
|
||||
return item.coerceToText(context).toString();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
android.text.ClipboardManager cm = (android.text.ClipboardManager)
|
||||
context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
if (cm.hasText())
|
||||
text = cm.getText().toString();
|
||||
android.text.ClipboardManager cm = (android.text.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
if (cm.hasText()) {
|
||||
return cm.getText().toString();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static SynchronousQueue<String> sClipboardQueue = new SynchronousQueue<String>();
|
||||
private static String EMPTY_STRING = new String();
|
||||
|
||||
static String getClipboardText() {
|
||||
// If we're on the UI thread or the background thread, we have a looper on the thread
|
||||
// and can just call this directly. For any other threads, post the call to the
|
||||
// background thread.
|
||||
|
||||
if (ThreadUtils.isOnUiThread() || ThreadUtils.isOnBackgroundThread()) {
|
||||
return getClipboardTextImpl();
|
||||
}
|
||||
|
||||
ThreadUtils.postToBackgroundThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String text = getClipboardTextImpl();
|
||||
try {
|
||||
sClipboardQueue.put(text != null ? text : EMPTY_STRING);
|
||||
} catch (InterruptedException ie) {}
|
||||
}});
|
||||
}
|
||||
});
|
||||
try {
|
||||
String ret = sClipboardQueue.take();
|
||||
return (EMPTY_STRING.equals(ret) ? null : ret);
|
||||
|
@ -1167,7 +1177,8 @@ public class GeckoAppShell
|
|||
context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
cm.setText(text);
|
||||
}
|
||||
}});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void setNotificationClient(NotificationClient client) {
|
||||
|
|
|
@ -84,6 +84,10 @@ public final class ThreadUtils {
|
|||
return isOnThread(getUiThread());
|
||||
}
|
||||
|
||||
public static boolean isOnBackgroundThread() {
|
||||
return isOnThread(sBackgroundThread);
|
||||
}
|
||||
|
||||
public static boolean isOnThread(Thread thread) {
|
||||
return (Thread.currentThread().getId() == thread.getId());
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче