Merge pull request #2195 from k9mail/rework_ClipboardManager
Rework ClipboardManager to avoid leaking Context
This commit is contained in:
commit
7aa784f16d
1 changed files with 5 additions and 19 deletions
|
@ -8,30 +8,16 @@ import android.content.Context;
|
||||||
* Access the system clipboard using the new {@link ClipboardManager} introduced with API 11
|
* Access the system clipboard using the new {@link ClipboardManager} introduced with API 11
|
||||||
*/
|
*/
|
||||||
public class ClipboardManager {
|
public class ClipboardManager {
|
||||||
|
|
||||||
private static ClipboardManager sInstance = null;
|
|
||||||
|
|
||||||
public static ClipboardManager getInstance(Context context) {
|
public static ClipboardManager getInstance(Context context) {
|
||||||
Context appContext = context.getApplicationContext();
|
Context appContext = context.getApplicationContext();
|
||||||
|
return new ClipboardManager(appContext);
|
||||||
if (sInstance == null) {
|
|
||||||
sInstance = new ClipboardManager(appContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
return sInstance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected Context mContext;
|
private Context context;
|
||||||
|
|
||||||
/**
|
private ClipboardManager(Context context) {
|
||||||
* Constructor
|
this.context = context;
|
||||||
*
|
|
||||||
* @param context
|
|
||||||
* A {@link Context} instance.
|
|
||||||
*/
|
|
||||||
protected ClipboardManager(Context context) {
|
|
||||||
mContext = context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,7 +30,7 @@ public class ClipboardManager {
|
||||||
*/
|
*/
|
||||||
public void setText(String label, String text) {
|
public void setText(String label, String text) {
|
||||||
android.content.ClipboardManager clipboardManager =
|
android.content.ClipboardManager clipboardManager =
|
||||||
(android.content.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
|
(android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
ClipData clip = ClipData.newPlainText(label, text);
|
ClipData clip = ClipData.newPlainText(label, text);
|
||||||
clipboardManager.setPrimaryClip(clip);
|
clipboardManager.setPrimaryClip(clip);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue