1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.content; 18 19 import android.app.Activity; 20 import android.app.ActivityManagerNative; 21 import android.app.LoadedApk; 22 import android.content.pm.ApplicationInfo; 23 import android.content.pm.PackageManager; 24 import android.content.res.AssetManager; 25 import android.content.res.Configuration; 26 import android.content.res.Resources; 27 import android.database.DatabaseErrorHandler; 28 import android.database.sqlite.SQLiteDatabase; 29 import android.database.sqlite.SQLiteDatabase.CursorFactory; 30 import android.graphics.Bitmap; 31 import android.graphics.drawable.Drawable; 32 import android.net.Uri; 33 import android.os.Bundle; 34 import android.os.Handler; 35 import android.os.Looper; 36 import android.os.RemoteException; 37 import android.os.UserHandle; 38 import android.view.DisplayAdjustments; 39 import android.view.Display; 40 41 import java.io.File; 42 import java.io.FileInputStream; 43 import java.io.FileNotFoundException; 44 import java.io.FileOutputStream; 45 import java.io.IOException; 46 import java.io.InputStream; 47 48 /** 49 * Proxying implementation of Context that simply delegates all of its calls to 50 * another Context. Can be subclassed to modify behavior without changing 51 * the original Context. 52 */ 53 public class ContextWrapper extends Context { 54 Context mBase; 55 ContextWrapper(Context base)56 public ContextWrapper(Context base) { 57 mBase = base; 58 } 59 60 /** 61 * Set the base context for this ContextWrapper. All calls will then be 62 * delegated to the base context. Throws 63 * IllegalStateException if a base context has already been set. 64 * 65 * @param base The new base context for this wrapper. 66 */ attachBaseContext(Context base)67 protected void attachBaseContext(Context base) { 68 if (mBase != null) { 69 throw new IllegalStateException("Base context already set"); 70 } 71 mBase = base; 72 } 73 74 /** 75 * @return the base context as set by the constructor or setBaseContext 76 */ getBaseContext()77 public Context getBaseContext() { 78 return mBase; 79 } 80 81 @Override getAssets()82 public AssetManager getAssets() { 83 return mBase.getAssets(); 84 } 85 86 @Override getResources()87 public Resources getResources() 88 { 89 return mBase.getResources(); 90 } 91 92 @Override getPackageManager()93 public PackageManager getPackageManager() { 94 return mBase.getPackageManager(); 95 } 96 97 @Override getContentResolver()98 public ContentResolver getContentResolver() { 99 return mBase.getContentResolver(); 100 } 101 102 @Override getMainLooper()103 public Looper getMainLooper() { 104 return mBase.getMainLooper(); 105 } 106 107 @Override getApplicationContext()108 public Context getApplicationContext() { 109 return mBase.getApplicationContext(); 110 } 111 112 @Override setTheme(int resid)113 public void setTheme(int resid) { 114 mBase.setTheme(resid); 115 } 116 117 /** @hide */ 118 @Override getThemeResId()119 public int getThemeResId() { 120 return mBase.getThemeResId(); 121 } 122 123 @Override getTheme()124 public Resources.Theme getTheme() { 125 return mBase.getTheme(); 126 } 127 128 @Override getClassLoader()129 public ClassLoader getClassLoader() { 130 return mBase.getClassLoader(); 131 } 132 133 @Override getPackageName()134 public String getPackageName() { 135 return mBase.getPackageName(); 136 } 137 138 /** @hide */ 139 @Override getBasePackageName()140 public String getBasePackageName() { 141 return mBase.getBasePackageName(); 142 } 143 144 /** @hide */ 145 @Override getOpPackageName()146 public String getOpPackageName() { 147 return mBase.getOpPackageName(); 148 } 149 150 @Override getApplicationInfo()151 public ApplicationInfo getApplicationInfo() { 152 return mBase.getApplicationInfo(); 153 } 154 155 @Override getPackageResourcePath()156 public String getPackageResourcePath() { 157 return mBase.getPackageResourcePath(); 158 } 159 160 @Override getPackageCodePath()161 public String getPackageCodePath() { 162 return mBase.getPackageCodePath(); 163 } 164 165 /** @hide */ 166 @Override getSharedPrefsFile(String name)167 public File getSharedPrefsFile(String name) { 168 return mBase.getSharedPrefsFile(name); 169 } 170 171 @Override getSharedPreferences(String name, int mode)172 public SharedPreferences getSharedPreferences(String name, int mode) { 173 return mBase.getSharedPreferences(name, mode); 174 } 175 176 @Override openFileInput(String name)177 public FileInputStream openFileInput(String name) 178 throws FileNotFoundException { 179 return mBase.openFileInput(name); 180 } 181 182 @Override openFileOutput(String name, int mode)183 public FileOutputStream openFileOutput(String name, int mode) 184 throws FileNotFoundException { 185 return mBase.openFileOutput(name, mode); 186 } 187 188 @Override deleteFile(String name)189 public boolean deleteFile(String name) { 190 return mBase.deleteFile(name); 191 } 192 193 @Override getFileStreamPath(String name)194 public File getFileStreamPath(String name) { 195 return mBase.getFileStreamPath(name); 196 } 197 198 @Override fileList()199 public String[] fileList() { 200 return mBase.fileList(); 201 } 202 203 @Override getFilesDir()204 public File getFilesDir() { 205 return mBase.getFilesDir(); 206 } 207 208 @Override getExternalFilesDir(String type)209 public File getExternalFilesDir(String type) { 210 return mBase.getExternalFilesDir(type); 211 } 212 213 @Override getExternalFilesDirs(String type)214 public File[] getExternalFilesDirs(String type) { 215 return mBase.getExternalFilesDirs(type); 216 } 217 218 @Override getObbDir()219 public File getObbDir() { 220 return mBase.getObbDir(); 221 } 222 223 @Override getObbDirs()224 public File[] getObbDirs() { 225 return mBase.getObbDirs(); 226 } 227 228 @Override getCacheDir()229 public File getCacheDir() { 230 return mBase.getCacheDir(); 231 } 232 233 @Override getExternalCacheDir()234 public File getExternalCacheDir() { 235 return mBase.getExternalCacheDir(); 236 } 237 238 @Override getExternalCacheDirs()239 public File[] getExternalCacheDirs() { 240 return mBase.getExternalCacheDirs(); 241 } 242 243 @Override getDir(String name, int mode)244 public File getDir(String name, int mode) { 245 return mBase.getDir(name, mode); 246 } 247 248 @Override openOrCreateDatabase(String name, int mode, CursorFactory factory)249 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) { 250 return mBase.openOrCreateDatabase(name, mode, factory); 251 } 252 253 @Override openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler)254 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, 255 DatabaseErrorHandler errorHandler) { 256 return mBase.openOrCreateDatabase(name, mode, factory, errorHandler); 257 } 258 259 @Override deleteDatabase(String name)260 public boolean deleteDatabase(String name) { 261 return mBase.deleteDatabase(name); 262 } 263 264 @Override getDatabasePath(String name)265 public File getDatabasePath(String name) { 266 return mBase.getDatabasePath(name); 267 } 268 269 @Override databaseList()270 public String[] databaseList() { 271 return mBase.databaseList(); 272 } 273 274 @Override getWallpaper()275 public Drawable getWallpaper() { 276 return mBase.getWallpaper(); 277 } 278 279 @Override peekWallpaper()280 public Drawable peekWallpaper() { 281 return mBase.peekWallpaper(); 282 } 283 284 @Override getWallpaperDesiredMinimumWidth()285 public int getWallpaperDesiredMinimumWidth() { 286 return mBase.getWallpaperDesiredMinimumWidth(); 287 } 288 289 @Override getWallpaperDesiredMinimumHeight()290 public int getWallpaperDesiredMinimumHeight() { 291 return mBase.getWallpaperDesiredMinimumHeight(); 292 } 293 294 @Override setWallpaper(Bitmap bitmap)295 public void setWallpaper(Bitmap bitmap) throws IOException { 296 mBase.setWallpaper(bitmap); 297 } 298 299 @Override setWallpaper(InputStream data)300 public void setWallpaper(InputStream data) throws IOException { 301 mBase.setWallpaper(data); 302 } 303 304 @Override clearWallpaper()305 public void clearWallpaper() throws IOException { 306 mBase.clearWallpaper(); 307 } 308 309 @Override startActivity(Intent intent)310 public void startActivity(Intent intent) { 311 mBase.startActivity(intent); 312 } 313 314 /** @hide */ 315 @Override startActivityAsUser(Intent intent, UserHandle user)316 public void startActivityAsUser(Intent intent, UserHandle user) { 317 mBase.startActivityAsUser(intent, user); 318 } 319 320 @Override startActivity(Intent intent, Bundle options)321 public void startActivity(Intent intent, Bundle options) { 322 mBase.startActivity(intent, options); 323 } 324 325 /** @hide */ 326 @Override startActivityAsUser(Intent intent, Bundle options, UserHandle user)327 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) { 328 mBase.startActivityAsUser(intent, options, user); 329 } 330 331 @Override startActivities(Intent[] intents)332 public void startActivities(Intent[] intents) { 333 mBase.startActivities(intents); 334 } 335 336 @Override startActivities(Intent[] intents, Bundle options)337 public void startActivities(Intent[] intents, Bundle options) { 338 mBase.startActivities(intents, options); 339 } 340 341 /** @hide */ 342 @Override startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle)343 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) { 344 mBase.startActivitiesAsUser(intents, options, userHandle); 345 } 346 347 @Override startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)348 public void startIntentSender(IntentSender intent, 349 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 350 throws IntentSender.SendIntentException { 351 mBase.startIntentSender(intent, fillInIntent, flagsMask, 352 flagsValues, extraFlags); 353 } 354 355 @Override startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)356 public void startIntentSender(IntentSender intent, 357 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, 358 Bundle options) throws IntentSender.SendIntentException { 359 mBase.startIntentSender(intent, fillInIntent, flagsMask, 360 flagsValues, extraFlags, options); 361 } 362 363 @Override sendBroadcast(Intent intent)364 public void sendBroadcast(Intent intent) { 365 mBase.sendBroadcast(intent); 366 } 367 368 @Override sendBroadcast(Intent intent, String receiverPermission)369 public void sendBroadcast(Intent intent, String receiverPermission) { 370 mBase.sendBroadcast(intent, receiverPermission); 371 } 372 373 /** @hide */ 374 @Override sendBroadcast(Intent intent, String receiverPermission, int appOp)375 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) { 376 mBase.sendBroadcast(intent, receiverPermission, appOp); 377 } 378 379 @Override sendOrderedBroadcast(Intent intent, String receiverPermission)380 public void sendOrderedBroadcast(Intent intent, 381 String receiverPermission) { 382 mBase.sendOrderedBroadcast(intent, receiverPermission); 383 } 384 385 @Override sendOrderedBroadcast( Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)386 public void sendOrderedBroadcast( 387 Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, 388 Handler scheduler, int initialCode, String initialData, 389 Bundle initialExtras) { 390 mBase.sendOrderedBroadcast(intent, receiverPermission, 391 resultReceiver, scheduler, initialCode, 392 initialData, initialExtras); 393 } 394 395 /** @hide */ 396 @Override sendOrderedBroadcast( Intent intent, String receiverPermission, int appOp, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)397 public void sendOrderedBroadcast( 398 Intent intent, String receiverPermission, int appOp, BroadcastReceiver resultReceiver, 399 Handler scheduler, int initialCode, String initialData, 400 Bundle initialExtras) { 401 mBase.sendOrderedBroadcast(intent, receiverPermission, appOp, 402 resultReceiver, scheduler, initialCode, 403 initialData, initialExtras); 404 } 405 406 @Override sendBroadcastAsUser(Intent intent, UserHandle user)407 public void sendBroadcastAsUser(Intent intent, UserHandle user) { 408 mBase.sendBroadcastAsUser(intent, user); 409 } 410 411 @Override sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission)412 public void sendBroadcastAsUser(Intent intent, UserHandle user, 413 String receiverPermission) { 414 mBase.sendBroadcastAsUser(intent, user, receiverPermission); 415 } 416 417 @Override sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)418 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 419 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, 420 int initialCode, String initialData, Bundle initialExtras) { 421 mBase.sendOrderedBroadcastAsUser(intent, user, receiverPermission, resultReceiver, 422 scheduler, initialCode, initialData, initialExtras); 423 } 424 425 @Override sendStickyBroadcast(Intent intent)426 public void sendStickyBroadcast(Intent intent) { 427 mBase.sendStickyBroadcast(intent); 428 } 429 430 @Override sendStickyOrderedBroadcast( Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)431 public void sendStickyOrderedBroadcast( 432 Intent intent, BroadcastReceiver resultReceiver, 433 Handler scheduler, int initialCode, String initialData, 434 Bundle initialExtras) { 435 mBase.sendStickyOrderedBroadcast(intent, 436 resultReceiver, scheduler, initialCode, 437 initialData, initialExtras); 438 } 439 440 @Override removeStickyBroadcast(Intent intent)441 public void removeStickyBroadcast(Intent intent) { 442 mBase.removeStickyBroadcast(intent); 443 } 444 445 @Override sendStickyBroadcastAsUser(Intent intent, UserHandle user)446 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { 447 mBase.sendStickyBroadcastAsUser(intent, user); 448 } 449 450 @Override sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle user, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)451 public void sendStickyOrderedBroadcastAsUser(Intent intent, 452 UserHandle user, BroadcastReceiver resultReceiver, 453 Handler scheduler, int initialCode, String initialData, 454 Bundle initialExtras) { 455 mBase.sendStickyOrderedBroadcastAsUser(intent, user, resultReceiver, 456 scheduler, initialCode, initialData, initialExtras); 457 } 458 459 @Override removeStickyBroadcastAsUser(Intent intent, UserHandle user)460 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) { 461 mBase.removeStickyBroadcastAsUser(intent, user); 462 } 463 464 @Override registerReceiver( BroadcastReceiver receiver, IntentFilter filter)465 public Intent registerReceiver( 466 BroadcastReceiver receiver, IntentFilter filter) { 467 return mBase.registerReceiver(receiver, filter); 468 } 469 470 @Override registerReceiver( BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler)471 public Intent registerReceiver( 472 BroadcastReceiver receiver, IntentFilter filter, 473 String broadcastPermission, Handler scheduler) { 474 return mBase.registerReceiver(receiver, filter, broadcastPermission, 475 scheduler); 476 } 477 478 /** @hide */ 479 @Override registerReceiverAsUser( BroadcastReceiver receiver, UserHandle user, IntentFilter filter, String broadcastPermission, Handler scheduler)480 public Intent registerReceiverAsUser( 481 BroadcastReceiver receiver, UserHandle user, IntentFilter filter, 482 String broadcastPermission, Handler scheduler) { 483 return mBase.registerReceiverAsUser(receiver, user, filter, broadcastPermission, 484 scheduler); 485 } 486 487 @Override unregisterReceiver(BroadcastReceiver receiver)488 public void unregisterReceiver(BroadcastReceiver receiver) { 489 mBase.unregisterReceiver(receiver); 490 } 491 492 @Override startService(Intent service)493 public ComponentName startService(Intent service) { 494 return mBase.startService(service); 495 } 496 497 @Override stopService(Intent name)498 public boolean stopService(Intent name) { 499 return mBase.stopService(name); 500 } 501 502 /** @hide */ 503 @Override startServiceAsUser(Intent service, UserHandle user)504 public ComponentName startServiceAsUser(Intent service, UserHandle user) { 505 return mBase.startServiceAsUser(service, user); 506 } 507 508 /** @hide */ 509 @Override stopServiceAsUser(Intent name, UserHandle user)510 public boolean stopServiceAsUser(Intent name, UserHandle user) { 511 return mBase.stopServiceAsUser(name, user); 512 } 513 514 @Override bindService(Intent service, ServiceConnection conn, int flags)515 public boolean bindService(Intent service, ServiceConnection conn, 516 int flags) { 517 return mBase.bindService(service, conn, flags); 518 } 519 520 /** @hide */ 521 @Override bindServiceAsUser(Intent service, ServiceConnection conn, int flags, UserHandle user)522 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, 523 UserHandle user) { 524 return mBase.bindServiceAsUser(service, conn, flags, user); 525 } 526 527 @Override unbindService(ServiceConnection conn)528 public void unbindService(ServiceConnection conn) { 529 mBase.unbindService(conn); 530 } 531 532 @Override startInstrumentation(ComponentName className, String profileFile, Bundle arguments)533 public boolean startInstrumentation(ComponentName className, 534 String profileFile, Bundle arguments) { 535 return mBase.startInstrumentation(className, profileFile, arguments); 536 } 537 538 @Override getSystemService(String name)539 public Object getSystemService(String name) { 540 return mBase.getSystemService(name); 541 } 542 543 @Override checkPermission(String permission, int pid, int uid)544 public int checkPermission(String permission, int pid, int uid) { 545 return mBase.checkPermission(permission, pid, uid); 546 } 547 548 @Override checkCallingPermission(String permission)549 public int checkCallingPermission(String permission) { 550 return mBase.checkCallingPermission(permission); 551 } 552 553 @Override checkCallingOrSelfPermission(String permission)554 public int checkCallingOrSelfPermission(String permission) { 555 return mBase.checkCallingOrSelfPermission(permission); 556 } 557 558 @Override enforcePermission( String permission, int pid, int uid, String message)559 public void enforcePermission( 560 String permission, int pid, int uid, String message) { 561 mBase.enforcePermission(permission, pid, uid, message); 562 } 563 564 @Override enforceCallingPermission(String permission, String message)565 public void enforceCallingPermission(String permission, String message) { 566 mBase.enforceCallingPermission(permission, message); 567 } 568 569 @Override enforceCallingOrSelfPermission( String permission, String message)570 public void enforceCallingOrSelfPermission( 571 String permission, String message) { 572 mBase.enforceCallingOrSelfPermission(permission, message); 573 } 574 575 @Override grantUriPermission(String toPackage, Uri uri, int modeFlags)576 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { 577 mBase.grantUriPermission(toPackage, uri, modeFlags); 578 } 579 580 @Override revokeUriPermission(Uri uri, int modeFlags)581 public void revokeUriPermission(Uri uri, int modeFlags) { 582 mBase.revokeUriPermission(uri, modeFlags); 583 } 584 585 @Override checkUriPermission(Uri uri, int pid, int uid, int modeFlags)586 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 587 return mBase.checkUriPermission(uri, pid, uid, modeFlags); 588 } 589 590 @Override checkCallingUriPermission(Uri uri, int modeFlags)591 public int checkCallingUriPermission(Uri uri, int modeFlags) { 592 return mBase.checkCallingUriPermission(uri, modeFlags); 593 } 594 595 @Override checkCallingOrSelfUriPermission(Uri uri, int modeFlags)596 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { 597 return mBase.checkCallingOrSelfUriPermission(uri, modeFlags); 598 } 599 600 @Override checkUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags)601 public int checkUriPermission(Uri uri, String readPermission, 602 String writePermission, int pid, int uid, int modeFlags) { 603 return mBase.checkUriPermission(uri, readPermission, writePermission, 604 pid, uid, modeFlags); 605 } 606 607 @Override enforceUriPermission( Uri uri, int pid, int uid, int modeFlags, String message)608 public void enforceUriPermission( 609 Uri uri, int pid, int uid, int modeFlags, String message) { 610 mBase.enforceUriPermission(uri, pid, uid, modeFlags, message); 611 } 612 613 @Override enforceCallingUriPermission( Uri uri, int modeFlags, String message)614 public void enforceCallingUriPermission( 615 Uri uri, int modeFlags, String message) { 616 mBase.enforceCallingUriPermission(uri, modeFlags, message); 617 } 618 619 @Override enforceCallingOrSelfUriPermission( Uri uri, int modeFlags, String message)620 public void enforceCallingOrSelfUriPermission( 621 Uri uri, int modeFlags, String message) { 622 mBase.enforceCallingOrSelfUriPermission(uri, modeFlags, message); 623 } 624 625 @Override enforceUriPermission( Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags, String message)626 public void enforceUriPermission( 627 Uri uri, String readPermission, String writePermission, 628 int pid, int uid, int modeFlags, String message) { 629 mBase.enforceUriPermission( 630 uri, readPermission, writePermission, pid, uid, modeFlags, 631 message); 632 } 633 634 @Override createPackageContext(String packageName, int flags)635 public Context createPackageContext(String packageName, int flags) 636 throws PackageManager.NameNotFoundException { 637 return mBase.createPackageContext(packageName, flags); 638 } 639 640 /** @hide */ 641 @Override createPackageContextAsUser(String packageName, int flags, UserHandle user)642 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user) 643 throws PackageManager.NameNotFoundException { 644 return mBase.createPackageContextAsUser(packageName, flags, user); 645 } 646 647 /** @hide */ 648 @Override getUserId()649 public int getUserId() { 650 return mBase.getUserId(); 651 } 652 653 @Override createConfigurationContext(Configuration overrideConfiguration)654 public Context createConfigurationContext(Configuration overrideConfiguration) { 655 return mBase.createConfigurationContext(overrideConfiguration); 656 } 657 658 @Override createDisplayContext(Display display)659 public Context createDisplayContext(Display display) { 660 return mBase.createDisplayContext(display); 661 } 662 663 @Override isRestricted()664 public boolean isRestricted() { 665 return mBase.isRestricted(); 666 } 667 668 /** @hide */ 669 @Override getDisplayAdjustments(int displayId)670 public DisplayAdjustments getDisplayAdjustments(int displayId) { 671 return mBase.getDisplayAdjustments(displayId); 672 } 673 } 674