Skip to content

Commit 39612d2

Browse files
neobuddy89PMS22
authored andcommitted
Clipboard access toast: Improvements [1/2]
* Add settings observer. Duh? * Make string translatable. * Priortize alerts: 1 - show important alerts, 2 - show all alerts * Make use of Toast.cancel() @neobuddy89: Intentionally skipping commit " ClipboardService: keep track of which app pasted from which" It is weird commit which works sometimes and throws odd messages like "Whatsapp pasted from Whatsapp" Old implementation is better and this commit should make it smarter. Signed-off-by: Pranav Vashi <neobuddy89@gmail.com> Change-Id: I7c09d450db6200693d44044c7dbc93e12098364a
1 parent 0c5b9e2 commit 39612d2

4 files changed

Lines changed: 98 additions & 18 deletions

File tree

core/java/android/provider/Settings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5323,10 +5323,10 @@ public boolean validate(@Nullable String value) {
53235323
public static final String QS_QUICKBAR_COLUMNS = "qs_quickbar_columns";
53245324

53255325
/**
5326-
* Toggle for clipboard access toast.
5326+
* Clipboard access info
53275327
* @hide
53285328
*/
5329-
public static final String SHOW_CLIPBOARD_TOAST = "show_clipboard_toast";
5329+
public static final String CLIPBOARD_TOAST_INFO = "clipboard_toast_info";
53305330

53315331
/**
53325332
* Settings to backup. This is here so that it's in the same place as the settings

core/res/res/values/fh_strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@
4545

4646
<!-- Screen recorder -->
4747
<string name="global_action_screenrecord">Screenrecord</string>
48+
49+
<!-- Clipboard access toast -->
50+
<string name="app_clipboard_access_denied">%1$s tried to access the clipboard and was denied</string>
51+
<string name="app_clipboard_access_granted">%1$s tried to access the clipboard and was granted</string>
4852
</resources>

core/res/res/values/fh_symbols.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,8 @@
155155

156156
<!-- Post reset runnable for all clients -->
157157
<java-symbol type="array" name="config_blockPackagesSensorDrain" />
158+
159+
<!-- Clipboard access toast -->
160+
<java-symbol type="string" name="app_clipboard_access_denied" />
161+
<java-symbol type="string" name="app_clipboard_access_granted" />
158162
</resources>

services/core/java/com/android/server/clipboard/ClipboardService.java

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import android.content.pm.PackageManager;
4343
import android.content.pm.PackageManager.NameNotFoundException;
4444
import android.content.pm.UserInfo;
45+
import android.database.ContentObserver;
4546
import android.net.Uri;
4647
import android.os.Binder;
4748
import android.os.Handler;
@@ -180,6 +181,12 @@ public class ClipboardService extends SystemService {
180181

181182
private final SparseArray<PerUserClipboard> mClipboards = new SparseArray<>();
182183

184+
private SettingsObserver mSettingsObserver;
185+
private int mShouldToast;
186+
private Handler mHandler;
187+
private String mToastMessage;
188+
private Toast mToast;
189+
183190
/**
184191
* Instantiates the clipboard.
185192
*/
@@ -215,6 +222,30 @@ public void onHostClipboardUpdated(String contents){
215222
mHostMonitorThread = new Thread(mHostClipboardMonitor);
216223
mHostMonitorThread.start();
217224
}
225+
226+
mHandler = new Handler();
227+
228+
mSettingsObserver = new SettingsObserver();
229+
mSettingsObserver.update();
230+
}
231+
232+
private class SettingsObserver extends ContentObserver {
233+
SettingsObserver() {
234+
super(mHandler);
235+
getContext().getContentResolver().registerContentObserver(Settings.System.getUriFor(
236+
Settings.System.CLIPBOARD_TOAST_INFO),
237+
false, this, UserHandle.USER_ALL);
238+
}
239+
240+
@Override
241+
public void onChange(boolean selfChange) {
242+
update();
243+
}
244+
245+
public void update() {
246+
mShouldToast = Settings.System.getIntForUser(getContext().getContentResolver(),
247+
Settings.System.CLIPBOARD_TOAST_INFO, 0, UserHandle.USER_CURRENT);
248+
}
218249
}
219250

220251
@Override
@@ -743,43 +774,70 @@ private final void revokeUris(PerUserClipboard clipboard) {
743774
}
744775
}
745776

777+
private Runnable mRunnable = new Runnable() {
778+
@Override
779+
public void run() {
780+
String msg = mToastMessage;
781+
782+
if (msg.isEmpty())
783+
return;
784+
785+
if (mToast != null) {
786+
mToast.cancel();
787+
mToast = null;
788+
}
789+
mToast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
790+
mToast.show();
791+
}
792+
};
793+
746794
private boolean clipboardAccessAllowed(int op, String callingPackage, int uid,
747795
@UserIdInt int userId) {
748-
final String prefName = "show_clipboard_toast";
749-
boolean shouldToast = Settings.System.getInt(getContext().getContentResolver(), prefName, 0) == 1;
750-
if (shouldToast) {
751-
// create toast that app has tried to access clipboard
752-
final PackageManager pm = getContext().getPackageManager();
753-
ApplicationInfo ai;
796+
Context context = getContext();
797+
String applicationName = "(unknown)";
798+
mToastMessage = "";
799+
800+
if (mShouldToast > 0) {
801+
final PackageManager pm = context.getPackageManager();
802+
ApplicationInfo ai = null;
754803
try {
755804
ai = pm.getApplicationInfo(callingPackage, 0);
805+
if (ai != null) applicationName = pm.getApplicationLabel(ai).toString();
756806
} catch (final NameNotFoundException e) {
757-
ai = null;
807+
// Do nothing
758808
}
759-
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
760-
Handler handler = new Handler(Looper.getMainLooper());
761-
handler.post(new Runnable() {
762-
@Override
763-
public void run() {
764-
Toast.makeText(getContext(), applicationName + " tried to access the clipboard", Toast.LENGTH_SHORT).show();
765-
}
766-
});
767809
}
810+
768811
// Check the AppOp.
769812
if (mAppOps.noteOp(op, uid, callingPackage) != AppOpsManager.MODE_ALLOWED) {
813+
if (mShouldToast > 0) {
814+
mToastMessage = String.format(context.getResources().getString(com.android.internal.R.string.app_clipboard_access_denied),
815+
applicationName);
816+
mHandler.post(mRunnable);
817+
}
770818
return false;
771819
}
772820
// Shell can access the clipboard for testing purposes.
773821
if (mPm.checkPermission(android.Manifest.permission.READ_CLIPBOARD_IN_BACKGROUND,
774822
callingPackage) == PackageManager.PERMISSION_GRANTED) {
823+
if (mShouldToast > 1) {
824+
mToastMessage = String.format(context.getResources().getString(com.android.internal.R.string.app_clipboard_access_granted),
825+
applicationName);
826+
mHandler.post(mRunnable);
827+
}
775828
return true;
776829
}
777830
// The default IME is always allowed to access the clipboard.
778-
String defaultIme = Settings.Secure.getStringForUser(getContext().getContentResolver(),
831+
String defaultIme = Settings.Secure.getStringForUser(context.getContentResolver(),
779832
Settings.Secure.DEFAULT_INPUT_METHOD, userId);
780833
if (!TextUtils.isEmpty(defaultIme)) {
781834
final String imePkg = ComponentName.unflattenFromString(defaultIme).getPackageName();
782835
if (imePkg.equals(callingPackage)) {
836+
if (mShouldToast > 1) {
837+
mToastMessage = String.format(context.getResources().getString(com.android.internal.R.string.app_clipboard_access_granted),
838+
applicationName);
839+
mHandler.post(mRunnable);
840+
}
783841
return true;
784842
}
785843
}
@@ -816,9 +874,23 @@ public void run() {
816874
+ ", application is not in focus neither is a system service for "
817875
+ "user " + userId);
818876
}
877+
if (mShouldToast > 0 && allowed) {
878+
mToastMessage = String.format(context.getResources().getString(com.android.internal.R.string.app_clipboard_access_granted),
879+
applicationName);
880+
mHandler.post(mRunnable);
881+
} else if (mShouldToast > 0 && !allowed) {
882+
mToastMessage = String.format(context.getResources().getString(com.android.internal.R.string.app_clipboard_access_denied),
883+
applicationName);
884+
mHandler.post(mRunnable);
885+
}
819886
return allowed;
820887
case AppOpsManager.OP_WRITE_CLIPBOARD:
821888
// Writing is allowed without focus.
889+
if (mShouldToast > 0) {
890+
mToastMessage = String.format(context.getResources().getString(com.android.internal.R.string.app_clipboard_access_granted),
891+
applicationName);
892+
mHandler.post(mRunnable);
893+
}
822894
return true;
823895
default:
824896
throw new IllegalArgumentException("Unknown clipboard appop " + op);

0 commit comments

Comments
 (0)