| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content; |
| 18 |
|
| 19 |
import android.Manifest; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.RequiresPermission; |
| 23 |
import android.annotation.SystemApi; |
| 24 |
import android.annotation.SystemService; |
| 25 |
import android.annotation.TestApi; |
| 26 |
import android.compat.annotation.UnsupportedAppUsage; |
| 27 |
import android.os.Handler; |
| 28 |
import android.os.RemoteException; |
| 29 |
import android.os.ServiceManager; |
| 30 |
import android.os.ServiceManager.ServiceNotFoundException; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.Objects; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
@SystemService(Context.CLIPBOARD_SERVICE) |
| 53 |
public class ClipboardManager extends android.text.ClipboardManager { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static final String DEVICE_CONFIG_SHOW_ACCESS_NOTIFICATIONS = |
| 63 |
"show_access_notifications"; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static final boolean DEVICE_CONFIG_DEFAULT_SHOW_ACCESS_NOTIFICATIONS = true; |
| 72 |
|
| 73 |
private final Context mContext; |
| 74 |
private final Handler mHandler; |
| 75 |
private final IClipboard mService; |
| 76 |
|
| 77 |
private final ArrayList<OnPrimaryClipChangedListener> mPrimaryClipChangedListeners |
| 78 |
= new ArrayList<OnPrimaryClipChangedListener>(); |
| 79 |
|
| 80 |
private final IOnPrimaryClipChangedListener.Stub mPrimaryClipChangedServiceListener |
| 81 |
= new IOnPrimaryClipChangedListener.Stub() { |
| 82 |
@Override |
| 83 |
public void dispatchPrimaryClipChanged() { |
| 84 |
mHandler.post(() -> { |
| 85 |
reportPrimaryClipChanged(); |
| 86 |
}); |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public interface OnPrimaryClipChangedListener { |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
void onPrimaryClipChanged(); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
@UnsupportedAppUsage |
| 113 |
public ClipboardManager(Context context, Handler handler) throws ServiceNotFoundException { |
| 114 |
mContext = context; |
| 115 |
mHandler = handler; |
| 116 |
mService = IClipboard.Stub.asInterface( |
| 117 |
ServiceManager.getServiceOrThrow(Context.CLIPBOARD_SERVICE)); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public void setPrimaryClip(@NonNull ClipData clip) { |
| 129 |
try { |
| 130 |
Objects.requireNonNull(clip); |
| 131 |
clip.prepareToLeaveProcess(true); |
| 132 |
mService.setPrimaryClip( |
| 133 |
clip, |
| 134 |
mContext.getOpPackageName(), |
| 135 |
mContext.getAttributionTag(), |
| 136 |
mContext.getUserId()); |
| 137 |
} catch (RemoteException e) { |
| 138 |
throw e.rethrowFromSystemServer(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
@SystemApi |
| 154 |
@RequiresPermission(Manifest.permission.SET_CLIP_SOURCE) |
| 155 |
public void setPrimaryClipAsPackage(@NonNull ClipData clip, @NonNull String sourcePackage) { |
| 156 |
try { |
| 157 |
Objects.requireNonNull(clip); |
| 158 |
Objects.requireNonNull(sourcePackage); |
| 159 |
clip.prepareToLeaveProcess(true); |
| 160 |
mService.setPrimaryClipAsPackage( |
| 161 |
clip, |
| 162 |
mContext.getOpPackageName(), |
| 163 |
mContext.getAttributionTag(), |
| 164 |
mContext.getUserId(), |
| 165 |
sourcePackage); |
| 166 |
} catch (RemoteException e) { |
| 167 |
throw e.rethrowFromSystemServer(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public void clearPrimaryClip() { |
| 177 |
try { |
| 178 |
mService.clearPrimaryClip( |
| 179 |
mContext.getOpPackageName(), |
| 180 |
mContext.getAttributionTag(), |
| 181 |
mContext.getUserId()); |
| 182 |
} catch (RemoteException e) { |
| 183 |
throw e.rethrowFromSystemServer(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public @Nullable ClipData getPrimaryClip() { |
| 196 |
try { |
| 197 |
return mService.getPrimaryClip( |
| 198 |
mContext.getOpPackageName(), |
| 199 |
mContext.getAttributionTag(), |
| 200 |
mContext.getUserId()); |
| 201 |
} catch (RemoteException e) { |
| 202 |
throw e.rethrowFromSystemServer(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public @Nullable ClipDescription getPrimaryClipDescription() { |
| 216 |
try { |
| 217 |
return mService.getPrimaryClipDescription( |
| 218 |
mContext.getOpPackageName(), |
| 219 |
mContext.getAttributionTag(), |
| 220 |
mContext.getUserId()); |
| 221 |
} catch (RemoteException e) { |
| 222 |
throw e.rethrowFromSystemServer(); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public boolean hasPrimaryClip() { |
| 233 |
try { |
| 234 |
return mService.hasPrimaryClip( |
| 235 |
mContext.getOpPackageName(), |
| 236 |
mContext.getAttributionTag(), |
| 237 |
mContext.getUserId()); |
| 238 |
} catch (RemoteException e) { |
| 239 |
throw e.rethrowFromSystemServer(); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
public void addPrimaryClipChangedListener(OnPrimaryClipChangedListener what) { |
| 244 |
synchronized (mPrimaryClipChangedListeners) { |
| 245 |
if (mPrimaryClipChangedListeners.isEmpty()) { |
| 246 |
try { |
| 247 |
mService.addPrimaryClipChangedListener( |
| 248 |
mPrimaryClipChangedServiceListener, |
| 249 |
mContext.getOpPackageName(), |
| 250 |
mContext.getAttributionTag(), |
| 251 |
mContext.getUserId()); |
| 252 |
} catch (RemoteException e) { |
| 253 |
throw e.rethrowFromSystemServer(); |
| 254 |
} |
| 255 |
} |
| 256 |
mPrimaryClipChangedListeners.add(what); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
public void removePrimaryClipChangedListener(OnPrimaryClipChangedListener what) { |
| 261 |
synchronized (mPrimaryClipChangedListeners) { |
| 262 |
mPrimaryClipChangedListeners.remove(what); |
| 263 |
if (mPrimaryClipChangedListeners.isEmpty()) { |
| 264 |
try { |
| 265 |
mService.removePrimaryClipChangedListener( |
| 266 |
mPrimaryClipChangedServiceListener, |
| 267 |
mContext.getOpPackageName(), |
| 268 |
mContext.getAttributionTag(), |
| 269 |
mContext.getUserId()); |
| 270 |
} catch (RemoteException e) { |
| 271 |
throw e.rethrowFromSystemServer(); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
@Deprecated |
| 282 |
public CharSequence getText() { |
| 283 |
ClipData clip = getPrimaryClip(); |
| 284 |
if (clip != null && clip.getItemCount() > 0) { |
| 285 |
return clip.getItemAt(0).coerceToText(mContext); |
| 286 |
} |
| 287 |
return null; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
@Deprecated |
| 296 |
public void setText(CharSequence text) { |
| 297 |
setPrimaryClip(ClipData.newPlainText(null, text)); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
@Deprecated |
| 304 |
public boolean hasText() { |
| 305 |
try { |
| 306 |
return mService.hasClipboardText( |
| 307 |
mContext.getOpPackageName(), |
| 308 |
mContext.getAttributionTag(), |
| 309 |
mContext.getUserId()); |
| 310 |
} catch (RemoteException e) { |
| 311 |
throw e.rethrowFromSystemServer(); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
@TestApi |
| 322 |
@Nullable |
| 323 |
@RequiresPermission(Manifest.permission.SET_CLIP_SOURCE) |
| 324 |
public String getPrimaryClipSource() { |
| 325 |
try { |
| 326 |
return mService.getPrimaryClipSource( |
| 327 |
mContext.getOpPackageName(), |
| 328 |
mContext.getAttributionTag(), |
| 329 |
mContext.getUserId()); |
| 330 |
} catch (RemoteException e) { |
| 331 |
throw e.rethrowFromSystemServer(); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
@UnsupportedAppUsage |
| 336 |
void reportPrimaryClipChanged() { |
| 337 |
Object[] listeners; |
| 338 |
|
| 339 |
synchronized (mPrimaryClipChangedListeners) { |
| 340 |
final int N = mPrimaryClipChangedListeners.size(); |
| 341 |
if (N <= 0) { |
| 342 |
return; |
| 343 |
} |
| 344 |
listeners = mPrimaryClipChangedListeners.toArray(); |
| 345 |
} |
| 346 |
|
| 347 |
for (int i=0; i<listeners.length; i++) { |
| 348 |
((OnPrimaryClipChangedListener)listeners[i]).onPrimaryClipChanged(); |
| 349 |
} |
| 350 |
} |
| 351 |
} |