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.content.pm.ApplicationInfo; 20 import android.content.pm.PackageManager; 21 import android.content.res.AssetManager; 22 import android.content.res.Resources; 23 import android.database.DatabaseErrorHandler; 24 import android.database.sqlite.SQLiteDatabase; 25 import android.database.sqlite.SQLiteDatabase.CursorFactory; 26 import android.graphics.Bitmap; 27 import android.graphics.drawable.Drawable; 28 import android.net.Uri; 29 import android.os.Bundle; 30 import android.os.Handler; 31 import android.os.Looper; 32 33 import java.io.File; 34 import java.io.FileInputStream; 35 import java.io.FileNotFoundException; 36 import java.io.FileOutputStream; 37 import java.io.IOException; 38 import java.io.InputStream; 39 40 /** 41 * Proxying implementation of Context that simply delegates all of its calls to 42 * another Context. Can be subclassed to modify behavior without changing 43 * the original Context. 44 */ 45 public class ContextWrapper extends Context { 46 Context mBase; 47 ContextWrapper(Context base)48 public ContextWrapper(Context base) { 49 mBase = base; 50 } 51 52 /** 53 * Set the base context for this ContextWrapper. All calls will then be 54 * delegated to the base context. Throws 55 * IllegalStateException if a base context has already been set. 56 * 57 * @param base The new base context for this wrapper. 58 */ attachBaseContext(Context base)59 protected void attachBaseContext(Context base) { 60 if (mBase != null) { 61 throw new IllegalStateException("Base context already set"); 62 } 63 mBase = base; 64 } 65 66 /** 67 * @return the base context as set by the constructor or setBaseContext 68 */ getBaseContext()69 public Context getBaseContext() { 70 return mBase; 71 } 72 73 @Override getAssets()74 public AssetManager getAssets() { 75 return mBase.getAssets(); 76 } 77 78 @Override getResources()79 public Resources getResources() 80 { 81 return mBase.getResources(); 82 } 83 84 @Override getPackageManager()85 public PackageManager getPackageManager() { 86 return mBase.getPackageManager(); 87 } 88 89 @Override getContentResolver()90 public ContentResolver getContentResolver() { 91 return mBase.getContentResolver(); 92 } 93 94 @Override getMainLooper()95 public Looper getMainLooper() { 96 return mBase.getMainLooper(); 97 } 98 99 @Override getApplicationContext()100 public Context getApplicationContext() { 101 return mBase.getApplicationContext(); 102 } 103 104 @Override setTheme(int resid)105 public void setTheme(int resid) { 106 mBase.setTheme(resid); 107 } 108 109 /** @hide */ 110 @Override getThemeResId()111 public int getThemeResId() { 112 return mBase.getThemeResId(); 113 } 114 115 @Override getTheme()116 public Resources.Theme getTheme() { 117 return mBase.getTheme(); 118 } 119 120 @Override getClassLoader()121 public ClassLoader getClassLoader() { 122 return mBase.getClassLoader(); 123 } 124 125 @Override getPackageName()126 public String getPackageName() { 127 return mBase.getPackageName(); 128 } 129 130 @Override getApplicationInfo()131 public ApplicationInfo getApplicationInfo() { 132 return mBase.getApplicationInfo(); 133 } 134 135 @Override getPackageResourcePath()136 public String getPackageResourcePath() { 137 return mBase.getPackageResourcePath(); 138 } 139 140 @Override getPackageCodePath()141 public String getPackageCodePath() { 142 return mBase.getPackageCodePath(); 143 } 144 145 /** @hide */ 146 @Override getSharedPrefsFile(String name)147 public File getSharedPrefsFile(String name) { 148 return mBase.getSharedPrefsFile(name); 149 } 150 151 @Override getSharedPreferences(String name, int mode)152 public SharedPreferences getSharedPreferences(String name, int mode) { 153 return mBase.getSharedPreferences(name, mode); 154 } 155 156 @Override openFileInput(String name)157 public FileInputStream openFileInput(String name) 158 throws FileNotFoundException { 159 return mBase.openFileInput(name); 160 } 161 162 @Override openFileOutput(String name, int mode)163 public FileOutputStream openFileOutput(String name, int mode) 164 throws FileNotFoundException { 165 return mBase.openFileOutput(name, mode); 166 } 167 168 @Override deleteFile(String name)169 public boolean deleteFile(String name) { 170 return mBase.deleteFile(name); 171 } 172 173 @Override getFileStreamPath(String name)174 public File getFileStreamPath(String name) { 175 return mBase.getFileStreamPath(name); 176 } 177 178 @Override fileList()179 public String[] fileList() { 180 return mBase.fileList(); 181 } 182 183 @Override getFilesDir()184 public File getFilesDir() { 185 return mBase.getFilesDir(); 186 } 187 188 @Override getExternalFilesDir(String type)189 public File getExternalFilesDir(String type) { 190 return mBase.getExternalFilesDir(type); 191 } 192 193 @Override getObbDir()194 public File getObbDir() { 195 return mBase.getObbDir(); 196 } 197 198 @Override getCacheDir()199 public File getCacheDir() { 200 return mBase.getCacheDir(); 201 } 202 203 @Override getExternalCacheDir()204 public File getExternalCacheDir() { 205 return mBase.getExternalCacheDir(); 206 } 207 208 @Override getDir(String name, int mode)209 public File getDir(String name, int mode) { 210 return mBase.getDir(name, mode); 211 } 212 213 @Override openOrCreateDatabase(String name, int mode, CursorFactory factory)214 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) { 215 return mBase.openOrCreateDatabase(name, mode, factory); 216 } 217 218 @Override openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler)219 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, 220 DatabaseErrorHandler errorHandler) { 221 return mBase.openOrCreateDatabase(name, mode, factory, errorHandler); 222 } 223 224 @Override deleteDatabase(String name)225 public boolean deleteDatabase(String name) { 226 return mBase.deleteDatabase(name); 227 } 228 229 @Override getDatabasePath(String name)230 public File getDatabasePath(String name) { 231 return mBase.getDatabasePath(name); 232 } 233 234 @Override databaseList()235 public String[] databaseList() { 236 return mBase.databaseList(); 237 } 238 239 @Override getWallpaper()240 public Drawable getWallpaper() { 241 return mBase.getWallpaper(); 242 } 243 244 @Override peekWallpaper()245 public Drawable peekWallpaper() { 246 return mBase.peekWallpaper(); 247 } 248 249 @Override getWallpaperDesiredMinimumWidth()250 public int getWallpaperDesiredMinimumWidth() { 251 return mBase.getWallpaperDesiredMinimumWidth(); 252 } 253 254 @Override getWallpaperDesiredMinimumHeight()255 public int getWallpaperDesiredMinimumHeight() { 256 return mBase.getWallpaperDesiredMinimumHeight(); 257 } 258 259 @Override setWallpaper(Bitmap bitmap)260 public void setWallpaper(Bitmap bitmap) throws IOException { 261 mBase.setWallpaper(bitmap); 262 } 263 264 @Override setWallpaper(InputStream data)265 public void setWallpaper(InputStream data) throws IOException { 266 mBase.setWallpaper(data); 267 } 268 269 @Override clearWallpaper()270 public void clearWallpaper() throws IOException { 271 mBase.clearWallpaper(); 272 } 273 274 @Override startActivity(Intent intent)275 public void startActivity(Intent intent) { 276 mBase.startActivity(intent); 277 } 278 279 @Override startActivities(Intent[] intents)280 public void startActivities(Intent[] intents) { 281 mBase.startActivities(intents); 282 } 283 284 @Override startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)285 public void startIntentSender(IntentSender intent, 286 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 287 throws IntentSender.SendIntentException { 288 mBase.startIntentSender(intent, fillInIntent, flagsMask, 289 flagsValues, extraFlags); 290 } 291 292 @Override sendBroadcast(Intent intent)293 public void sendBroadcast(Intent intent) { 294 mBase.sendBroadcast(intent); 295 } 296 297 @Override sendBroadcast(Intent intent, String receiverPermission)298 public void sendBroadcast(Intent intent, String receiverPermission) { 299 mBase.sendBroadcast(intent, receiverPermission); 300 } 301 302 @Override sendOrderedBroadcast(Intent intent, String receiverPermission)303 public void sendOrderedBroadcast(Intent intent, 304 String receiverPermission) { 305 mBase.sendOrderedBroadcast(intent, receiverPermission); 306 } 307 308 @Override sendOrderedBroadcast( Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)309 public void sendOrderedBroadcast( 310 Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, 311 Handler scheduler, int initialCode, String initialData, 312 Bundle initialExtras) { 313 mBase.sendOrderedBroadcast(intent, receiverPermission, 314 resultReceiver, scheduler, initialCode, 315 initialData, initialExtras); 316 } 317 318 @Override sendStickyBroadcast(Intent intent)319 public void sendStickyBroadcast(Intent intent) { 320 mBase.sendStickyBroadcast(intent); 321 } 322 323 @Override sendStickyOrderedBroadcast( Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)324 public void sendStickyOrderedBroadcast( 325 Intent intent, BroadcastReceiver resultReceiver, 326 Handler scheduler, int initialCode, String initialData, 327 Bundle initialExtras) { 328 mBase.sendStickyOrderedBroadcast(intent, 329 resultReceiver, scheduler, initialCode, 330 initialData, initialExtras); 331 } 332 333 @Override removeStickyBroadcast(Intent intent)334 public void removeStickyBroadcast(Intent intent) { 335 mBase.removeStickyBroadcast(intent); 336 } 337 338 @Override registerReceiver( BroadcastReceiver receiver, IntentFilter filter)339 public Intent registerReceiver( 340 BroadcastReceiver receiver, IntentFilter filter) { 341 return mBase.registerReceiver(receiver, filter); 342 } 343 344 @Override registerReceiver( BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler)345 public Intent registerReceiver( 346 BroadcastReceiver receiver, IntentFilter filter, 347 String broadcastPermission, Handler scheduler) { 348 return mBase.registerReceiver(receiver, filter, broadcastPermission, 349 scheduler); 350 } 351 352 @Override unregisterReceiver(BroadcastReceiver receiver)353 public void unregisterReceiver(BroadcastReceiver receiver) { 354 mBase.unregisterReceiver(receiver); 355 } 356 357 @Override startService(Intent service)358 public ComponentName startService(Intent service) { 359 return mBase.startService(service); 360 } 361 362 @Override stopService(Intent name)363 public boolean stopService(Intent name) { 364 return mBase.stopService(name); 365 } 366 367 @Override bindService(Intent service, ServiceConnection conn, int flags)368 public boolean bindService(Intent service, ServiceConnection conn, 369 int flags) { 370 return mBase.bindService(service, conn, flags); 371 } 372 373 @Override unbindService(ServiceConnection conn)374 public void unbindService(ServiceConnection conn) { 375 mBase.unbindService(conn); 376 } 377 378 @Override startInstrumentation(ComponentName className, String profileFile, Bundle arguments)379 public boolean startInstrumentation(ComponentName className, 380 String profileFile, Bundle arguments) { 381 return mBase.startInstrumentation(className, profileFile, arguments); 382 } 383 384 @Override getSystemService(String name)385 public Object getSystemService(String name) { 386 return mBase.getSystemService(name); 387 } 388 389 @Override checkPermission(String permission, int pid, int uid)390 public int checkPermission(String permission, int pid, int uid) { 391 return mBase.checkPermission(permission, pid, uid); 392 } 393 394 @Override checkCallingPermission(String permission)395 public int checkCallingPermission(String permission) { 396 return mBase.checkCallingPermission(permission); 397 } 398 399 @Override checkCallingOrSelfPermission(String permission)400 public int checkCallingOrSelfPermission(String permission) { 401 return mBase.checkCallingOrSelfPermission(permission); 402 } 403 404 @Override enforcePermission( String permission, int pid, int uid, String message)405 public void enforcePermission( 406 String permission, int pid, int uid, String message) { 407 mBase.enforcePermission(permission, pid, uid, message); 408 } 409 410 @Override enforceCallingPermission(String permission, String message)411 public void enforceCallingPermission(String permission, String message) { 412 mBase.enforceCallingPermission(permission, message); 413 } 414 415 @Override enforceCallingOrSelfPermission( String permission, String message)416 public void enforceCallingOrSelfPermission( 417 String permission, String message) { 418 mBase.enforceCallingOrSelfPermission(permission, message); 419 } 420 421 @Override grantUriPermission(String toPackage, Uri uri, int modeFlags)422 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { 423 mBase.grantUriPermission(toPackage, uri, modeFlags); 424 } 425 426 @Override revokeUriPermission(Uri uri, int modeFlags)427 public void revokeUriPermission(Uri uri, int modeFlags) { 428 mBase.revokeUriPermission(uri, modeFlags); 429 } 430 431 @Override checkUriPermission(Uri uri, int pid, int uid, int modeFlags)432 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 433 return mBase.checkUriPermission(uri, pid, uid, modeFlags); 434 } 435 436 @Override checkCallingUriPermission(Uri uri, int modeFlags)437 public int checkCallingUriPermission(Uri uri, int modeFlags) { 438 return mBase.checkCallingUriPermission(uri, modeFlags); 439 } 440 441 @Override checkCallingOrSelfUriPermission(Uri uri, int modeFlags)442 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { 443 return mBase.checkCallingOrSelfUriPermission(uri, modeFlags); 444 } 445 446 @Override checkUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags)447 public int checkUriPermission(Uri uri, String readPermission, 448 String writePermission, int pid, int uid, int modeFlags) { 449 return mBase.checkUriPermission(uri, readPermission, writePermission, 450 pid, uid, modeFlags); 451 } 452 453 @Override enforceUriPermission( Uri uri, int pid, int uid, int modeFlags, String message)454 public void enforceUriPermission( 455 Uri uri, int pid, int uid, int modeFlags, String message) { 456 mBase.enforceUriPermission(uri, pid, uid, modeFlags, message); 457 } 458 459 @Override enforceCallingUriPermission( Uri uri, int modeFlags, String message)460 public void enforceCallingUriPermission( 461 Uri uri, int modeFlags, String message) { 462 mBase.enforceCallingUriPermission(uri, modeFlags, message); 463 } 464 465 @Override enforceCallingOrSelfUriPermission( Uri uri, int modeFlags, String message)466 public void enforceCallingOrSelfUriPermission( 467 Uri uri, int modeFlags, String message) { 468 mBase.enforceCallingOrSelfUriPermission(uri, modeFlags, message); 469 } 470 471 @Override enforceUriPermission( Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags, String message)472 public void enforceUriPermission( 473 Uri uri, String readPermission, String writePermission, 474 int pid, int uid, int modeFlags, String message) { 475 mBase.enforceUriPermission( 476 uri, readPermission, writePermission, pid, uid, modeFlags, 477 message); 478 } 479 480 @Override createPackageContext(String packageName, int flags)481 public Context createPackageContext(String packageName, int flags) 482 throws PackageManager.NameNotFoundException { 483 return mBase.createPackageContext(packageName, flags); 484 } 485 486 @Override isRestricted()487 public boolean isRestricted() { 488 return mBase.isRestricted(); 489 } 490 } 491