Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: ui/android/java/src/org/chromium/ui/base/Clipboard.java

Issue 2772073002: Make Clipboard.java a singleton pattern. (Closed)
Patch Set: Fix shame Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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.ui.base; 5 package org.chromium.ui.base;
6 6
7 import android.content.ClipData; 7 import android.content.ClipData;
8 import android.content.ClipboardManager; 8 import android.content.ClipboardManager;
9 import android.content.Context; 9 import android.content.Context;
10 10
11 import org.chromium.base.ContextUtils;
11 import org.chromium.base.annotations.CalledByNative; 12 import org.chromium.base.annotations.CalledByNative;
12 import org.chromium.base.annotations.JNINamespace; 13 import org.chromium.base.annotations.JNINamespace;
13 import org.chromium.base.annotations.SuppressFBWarnings; 14 import org.chromium.base.annotations.SuppressFBWarnings;
14 import org.chromium.ui.R; 15 import org.chromium.ui.R;
15 import org.chromium.ui.widget.Toast; 16 import org.chromium.ui.widget.Toast;
16 17
17 /** 18 /**
18 * Simple proxy that provides C++ code with an access pathway to the Android 19 * Simple proxy that provides C++ code with an access pathway to the Android cli pboard.
19 * clipboard.
20 */ 20 */
21 @JNINamespace("ui") 21 @JNINamespace("ui")
22 public class Clipboard { 22 public class Clipboard {
23 private static Clipboard sInstance;
24
23 // Necessary for coercing clipboard contents to text if they require 25 // Necessary for coercing clipboard contents to text if they require
24 // access to network resources, etceteras (e.g., URI in clipboard) 26 // access to network resources, etceteras (e.g., URI in clipboard)
25 private final Context mContext; 27 private final Context mContext;
26 28
27 private final ClipboardManager mClipboardManager; 29 private final ClipboardManager mClipboardManager;
28 30
29 /** 31 /**
30 * Use the factory constructor instead. 32 * Get the singleton Clipboard instance (creating it if needed).
31 *
32 * @param context for accessing the clipboard
33 */ 33 */
34 public Clipboard(final Context context) { 34 @CalledByNative
35 mContext = context; 35 public static Clipboard getInstance() {
36 mClipboardManager = (ClipboardManager) 36 if (sInstance == null) {
37 context.getSystemService(Context.CLIPBOARD_SERVICE); 37 sInstance = new Clipboard();
38 }
39 return sInstance;
40 }
41
42 private Clipboard() {
43 mContext = ContextUtils.getApplicationContext();
44 mClipboardManager =
45 (ClipboardManager) ContextUtils.getApplicationContext().getSyste mService(
46 Context.CLIPBOARD_SERVICE);
38 } 47 }
39 48
40 /** 49 /**
41 * Returns a new Clipboard object bound to the specified context.
42 *
43 * @param context for accessing the clipboard
44 * @return the new object
45 */
46 @CalledByNative
47 private static Clipboard create(final Context context) {
48 return new Clipboard(context);
49 }
50
51 /**
52 * Emulates the behavior of the now-deprecated 50 * Emulates the behavior of the now-deprecated
53 * {@link android.text.ClipboardManager#getText()} by invoking 51 * {@link android.text.ClipboardManager#getText()} by invoking
54 * {@link android.content.ClipData.Item#coerceToText(Context)} on the first 52 * {@link android.content.ClipData.Item#coerceToText(Context)} on the first
55 * item in the clipboard (if any) and returning the result as a string. 53 * item in the clipboard (if any) and returning the result as a string.
56 * <p> 54 * <p>
57 * This is quite different than simply calling {@link Object#toString()} on 55 * This is quite different than simply calling {@link Object#toString()} on
58 * the clip; consumers of this API should familiarize themselves with the 56 * the clip; consumers of this API should familiarize themselves with the
59 * process described in 57 * process described in
60 * {@link android.content.ClipData.Item#coerceToText(Context)} before using 58 * {@link android.content.ClipData.Item#coerceToText(Context)} before using
61 * this method. 59 * this method.
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 private void setPrimaryClipNoException(ClipData clip) { 133 private void setPrimaryClipNoException(ClipData clip) {
136 try { 134 try {
137 mClipboardManager.setPrimaryClip(clip); 135 mClipboardManager.setPrimaryClip(clip);
138 } catch (Exception ex) { 136 } catch (Exception ex) {
139 // Ignore any exceptions here as certain devices have bugs and will fail. 137 // Ignore any exceptions here as certain devices have bugs and will fail.
140 String text = mContext.getString(R.string.copy_to_clipboard_failure_ message); 138 String text = mContext.getString(R.string.copy_to_clipboard_failure_ message);
141 Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show(); 139 Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
142 } 140 }
143 } 141 }
144 } 142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698