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