From d26924d8d7c29cbcbfaf43c408d3b54e50b7a7ce Mon Sep 17 00:00:00 2001 From: Fredrik Jagenheim Date: Fri, 12 Feb 2010 15:52:39 +0100 Subject: [PATCH 1/8] Corrected SPN and PLMN name handling SPDI-file on SIM and BCD encoded PLMN-entries wasn't parsed correctly causing us to display wrong networknames. Change-Id: I09d80deec45b4b3ad525a9359b4866de7549e39e --- .../java/com/android/internal/telephony/IccUtils.java | 2 ++ .../com/android/internal/telephony/gsm/SIMRecords.java | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/telephony/java/com/android/internal/telephony/IccUtils.java b/telephony/java/com/android/internal/telephony/IccUtils.java index 3e7094e606cb8..1034bda11912a 100644 --- a/telephony/java/com/android/internal/telephony/IccUtils.java +++ b/telephony/java/com/android/internal/telephony/IccUtils.java @@ -51,6 +51,8 @@ public class IccUtils { ret.append((char)('0' + v)); v = (data[i] >> 4) & 0xf; + // Some PLMNs have 'f' as high nibble, ignore it + if (v == 0xf) continue; if (v > 9) break; ret.append((char)('0' + v)); } diff --git a/telephony/java/com/android/internal/telephony/gsm/SIMRecords.java b/telephony/java/com/android/internal/telephony/gsm/SIMRecords.java index d711a80872626..d99a348674f41 100644 --- a/telephony/java/com/android/internal/telephony/gsm/SIMRecords.java +++ b/telephony/java/com/android/internal/telephony/gsm/SIMRecords.java @@ -93,6 +93,7 @@ public final class SIMRecords extends IccRecords { static final int SPN_RULE_SHOW_PLMN = 0x02; // From TS 51.011 EF[SPDI] section + static final int TAG_SPDI = 0xA3; static final int TAG_SPDI_PLMN_LIST = 0x80; // Full Name IEI from TS 24.008 @@ -1426,8 +1427,12 @@ public final class SIMRecords extends IccRecords { byte[] plmnEntries = null; - // There should only be one TAG_SPDI_PLMN_LIST for ( ; tlv.isValidObject() ; tlv.nextObject()) { + // Skip SPDI tag, if existant + if (tlv.getTag() == TAG_SPDI) { + tlv = new SimTlv(tlv.getData(), 0, tlv.getData().length); + } + // There should only be one TAG_SPDI_PLMN_LIST if (tlv.getTag() == TAG_SPDI_PLMN_LIST) { plmnEntries = tlv.getData(); break; From 232a69851d8f208a73d8b4ddb39708828ae9f205 Mon Sep 17 00:00:00 2001 From: Henrik Hall Date: Wed, 24 Feb 2010 12:52:06 +0100 Subject: [PATCH 2/8] Fix data type of RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS in RIL. Fixing incorrect data type of RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS in Android RIL. The Android RIL specifies that the unsolicited response signal RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS should contain a raw byte array (ril.h). However, in the implementation of the Java RIL and the C++ RIL daemon, the data is unpacked as a string. This causes data delivered from the network stack to be garbled. Changing so that both the C++ RIL deamon in platform/hardware/ril and the Java RIL in frameworks/base follows the specification. Change-Id: I9b9b26d16cd39422c43d9fa7aadfcff3e60c78ae --- telephony/java/com/android/internal/telephony/RIL.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telephony/java/com/android/internal/telephony/RIL.java b/telephony/java/com/android/internal/telephony/RIL.java index 7239889c16071..8ec1d61ffd920 100644 --- a/telephony/java/com/android/internal/telephony/RIL.java +++ b/telephony/java/com/android/internal/telephony/RIL.java @@ -2329,7 +2329,7 @@ public final class RIL extends BaseCommands implements CommandsInterface { case RIL_UNSOL_RESTRICTED_STATE_CHANGED: ret = responseInts(p); break; case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED: ret = responseVoid(p); break; case RIL_UNSOL_RESPONSE_CDMA_NEW_SMS: ret = responseCdmaSms(p); break; - case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS: ret = responseString(p); break; + case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS: ret = responseRaw(p); break; case RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL: ret = responseVoid(p); break; case RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE: ret = responseVoid(p); break; case RIL_UNSOL_CDMA_CALL_WAITING: ret = responseCdmaCallWaiting(p); break; From 4136c98ecc25861da5462d1364f1c4bfd7bd5c31 Mon Sep 17 00:00:00 2001 From: Christian Mehlmauer Date: Tue, 25 May 2010 20:04:09 +0200 Subject: [PATCH 3/8] Replaced deprecated String Constructor Change-Id: I91874093d69e230850c695b92da5f91391030d44 --- services/java/com/android/server/ProcessStats.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/java/com/android/server/ProcessStats.java b/services/java/com/android/server/ProcessStats.java index ac3b723544763..cbd3d8b86a126 100644 --- a/services/java/com/android/server/ProcessStats.java +++ b/services/java/com/android/server/ProcessStats.java @@ -687,7 +687,7 @@ public class ProcessStats { break; } } - return new String(mBuffer, 0, 0, i); + return new String(mBuffer, 0, i); } } catch (java.io.FileNotFoundException e) { } catch (java.io.IOException e) { From 8999515f66df3c856af4e51bb26da1cb799b14f4 Mon Sep 17 00:00:00 2001 From: Mathias Palmqvist Date: Wed, 2 Jun 2010 16:03:04 +0200 Subject: [PATCH 4/8] Use stride for source and target buffers when copybit is enabled. Change-Id: Ie651013b3522f8e004685d74190da86433086307 --- opengl/libagl/egl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opengl/libagl/egl.cpp b/opengl/libagl/egl.cpp index 81864bd791bce..9d6de72f4fac6 100644 --- a/opengl/libagl/egl.cpp +++ b/opengl/libagl/egl.cpp @@ -485,13 +485,13 @@ void egl_window_surface_v2_t::copyBlt( copybit_device_t* const copybit = blitengine; if (copybit) { copybit_image_t simg; - simg.w = src->width; + simg.w = src->stride; simg.h = src->height; simg.format = src->format; simg.handle = const_cast(src->handle); copybit_image_t dimg; - dimg.w = dst->width; + dimg.w = dst->stride; dimg.h = dst->height; dimg.format = dst->format; dimg.handle = const_cast(dst->handle); From baccb91ece4ac650cae465ae36f5d7397a1f32af Mon Sep 17 00:00:00 2001 From: Kenny Date: Wed, 2 Jun 2010 23:14:46 +0800 Subject: [PATCH 5/8] Fix the ssid display issue Reproduce steps: 1. Set a special AP name that contains "=" (looks like "-=^-^=-"); 2. Connect to this AP; 3. The display of ssid will be incorrect; Change-Id: Idc7433a42cf59932f5fb147f5500e0175f745399 --- wifi/java/android/net/wifi/WifiStateTracker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wifi/java/android/net/wifi/WifiStateTracker.java b/wifi/java/android/net/wifi/WifiStateTracker.java index c45b480c3af86..d0f0e718d8d68 100644 --- a/wifi/java/android/net/wifi/WifiStateTracker.java +++ b/wifi/java/android/net/wifi/WifiStateTracker.java @@ -1373,7 +1373,7 @@ public class WifiStateTracker extends NetworkStateTracker { int netId = -1; String[] lines = reply.split("\n"); for (String line : lines) { - String[] prop = line.split(" *= *"); + String[] prop = line.split(" *= *", 2); if (prop.length < 2) continue; String name = prop[0]; From e6af73e18eaa1303a1499b6ec240afd953a92607 Mon Sep 17 00:00:00 2001 From: Christian Mehlmauer Date: Tue, 25 May 2010 18:59:41 +0200 Subject: [PATCH 6/8] Replaced deprecated Inten.toURI method Change-Id: Iff3200294edbc1cac188502219bfe481133a5d8e --- core/java/android/app/ApplicationContext.java | 2 +- core/java/android/app/SearchDialog.java | 4 ++-- core/java/android/provider/Settings.java | 2 +- .../src/com/android/providers/settings/DatabaseHelper.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/java/android/app/ApplicationContext.java b/core/java/android/app/ApplicationContext.java index f48f15092f8d3..4c8e8ff1a1f50 100644 --- a/core/java/android/app/ApplicationContext.java +++ b/core/java/android/app/ApplicationContext.java @@ -1972,7 +1972,7 @@ class ApplicationContext extends Context { return info.activityInfo.loadIcon(this); } - throw new NameNotFoundException(intent.toURI()); + throw new NameNotFoundException(intent.toUri(0)); } @Override public Drawable getDefaultActivityIcon() { diff --git a/core/java/android/app/SearchDialog.java b/core/java/android/app/SearchDialog.java index e5a769bb59dd6..be4fa0ec2724d 100644 --- a/core/java/android/app/SearchDialog.java +++ b/core/java/android/app/SearchDialog.java @@ -1336,7 +1336,7 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS } else { // If the intent was created from a suggestion, it will always have an explicit // component here. - Log.i(LOG_TAG, "Starting (as ourselves) " + intent.toURI()); + Log.i(LOG_TAG, "Starting (as ourselves) " + intent.toUri(0)); getContext().startActivity(intent); // If the search switches to a different activity, // SearchDialogWrapper#performActivityResuming @@ -1418,7 +1418,7 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS String resultWho = null; int requestCode = -1; boolean onlyIfNeeded = false; - Log.i(LOG_TAG, "Starting (uid " + uid + ", " + packageName + ") " + intent.toURI()); + Log.i(LOG_TAG, "Starting (uid " + uid + ", " + packageName + ") " + intent.toUri(0)); int result = ActivityManagerNative.getDefault().startActivityInPackage( uid, intent, resolvedType, resultTo, resultWho, requestCode, onlyIfNeeded); checkStartActivityResult(result, intent); diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index 69d3b77385550..6fbb3e9e7f055 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -3837,7 +3837,7 @@ public final class Settings { ContentValues values = new ContentValues(); if (title != null) values.put(TITLE, title); if (folder != null) values.put(FOLDER, folder); - values.put(INTENT, intent.toURI()); + values.put(INTENT, intent.toUri(0)); if (shortcut != 0) values.put(SHORTCUT, (int) shortcut); values.put(ORDERING, ordering); return cr.insert(CONTENT_URI, values); diff --git a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java index 1a64e20b1c99b..f8f695c1ea638 100644 --- a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java +++ b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java @@ -637,7 +637,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { info = packageManager.getActivityInfo(cn, 0); intent.setComponent(cn); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - values.put(Settings.Bookmarks.INTENT, intent.toURI()); + values.put(Settings.Bookmarks.INTENT, intent.toUri(0)); values.put(Settings.Bookmarks.TITLE, info.loadLabel(packageManager).toString()); values.put(Settings.Bookmarks.SHORTCUT, shortcutValue); From a191eb013a491a6319b4f7ac51d4b0b16b3a8f70 Mon Sep 17 00:00:00 2001 From: The Android Open Source Project Date: Tue, 15 Jun 2010 09:59:15 -0700 Subject: [PATCH 7/8] Fix build This file was brought back to life by mistaken when merging from a branch where it was modified into a branch where it had been deleted Change-Id: I406c6ced978abfdd59684f14bc9dd53ee7968aae --- core/java/android/app/ApplicationContext.java | 2781 ----------------- 1 file changed, 2781 deletions(-) delete mode 100644 core/java/android/app/ApplicationContext.java diff --git a/core/java/android/app/ApplicationContext.java b/core/java/android/app/ApplicationContext.java deleted file mode 100644 index 4c8e8ff1a1f50..0000000000000 --- a/core/java/android/app/ApplicationContext.java +++ /dev/null @@ -1,2781 +0,0 @@ -/* - * Copyright (C) 2006 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.app; - -import com.android.internal.policy.PolicyManager; -import com.android.internal.util.XmlUtils; -import com.google.android.collect.Maps; - -import org.xmlpull.v1.XmlPullParserException; - -import android.content.BroadcastReceiver; -import android.content.ComponentName; -import android.content.ContentResolver; -import android.content.Context; -import android.content.ContextWrapper; -import android.content.IContentProvider; -import android.content.Intent; -import android.content.IntentFilter; -import android.content.IIntentReceiver; -import android.content.IntentSender; -import android.content.ReceiverCallNotAllowedException; -import android.content.ServiceConnection; -import android.content.SharedPreferences; -import android.content.pm.ActivityInfo; -import android.content.pm.ApplicationInfo; -import android.content.pm.ComponentInfo; -import android.content.pm.FeatureInfo; -import android.content.pm.IPackageDataObserver; -import android.content.pm.IPackageDeleteObserver; -import android.content.pm.IPackageInstallObserver; -import android.content.pm.IPackageManager; -import android.content.pm.IPackageStatsObserver; -import android.content.pm.InstrumentationInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.pm.PermissionGroupInfo; -import android.content.pm.PermissionInfo; -import android.content.pm.ProviderInfo; -import android.content.pm.ResolveInfo; -import android.content.pm.ServiceInfo; -import android.content.res.AssetManager; -import android.content.res.Resources; -import android.content.res.XmlResourceParser; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteDatabase.CursorFactory; -import android.graphics.Bitmap; -import android.graphics.drawable.Drawable; -import android.hardware.SensorManager; -import android.location.ILocationManager; -import android.location.LocationManager; -import android.media.AudioManager; -import android.net.ConnectivityManager; -import android.net.IConnectivityManager; -import android.net.Uri; -import android.net.wifi.IWifiManager; -import android.net.wifi.WifiManager; -import android.os.Binder; -import android.os.Bundle; -import android.os.FileUtils; -import android.os.Handler; -import android.os.IBinder; -import android.os.IPowerManager; -import android.os.Looper; -import android.os.PowerManager; -import android.os.Process; -import android.os.RemoteException; -import android.os.ServiceManager; -import android.os.Vibrator; -import android.os.FileUtils.FileStatus; -import android.telephony.TelephonyManager; -import android.text.ClipboardManager; -import android.util.AndroidRuntimeException; -import android.util.Log; -import android.view.ContextThemeWrapper; -import android.view.LayoutInflater; -import android.view.WindowManagerImpl; -import android.view.accessibility.AccessibilityManager; -import android.view.inputmethod.InputMethodManager; -import android.accounts.AccountManager; -import android.accounts.IAccountManager; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.WeakHashMap; -import java.util.Map.Entry; - -class ReceiverRestrictedContext extends ContextWrapper { - ReceiverRestrictedContext(Context base) { - super(base); - } - - @Override - public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { - return registerReceiver(receiver, filter, null, null); - } - - @Override - public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, - String broadcastPermission, Handler scheduler) { - throw new ReceiverCallNotAllowedException( - "IntentReceiver components are not allowed to register to receive intents"); - //ex.fillInStackTrace(); - //Log.e("IntentReceiver", ex.getMessage(), ex); - //return mContext.registerReceiver(receiver, filter, broadcastPermission, - // scheduler); - } - - @Override - public boolean bindService(Intent service, ServiceConnection conn, int flags) { - throw new ReceiverCallNotAllowedException( - "IntentReceiver components are not allowed to bind to services"); - //ex.fillInStackTrace(); - //Log.e("IntentReceiver", ex.getMessage(), ex); - //return mContext.bindService(service, interfaceName, conn, flags); - } -} - -/** - * Common implementation of Context API, which Activity and other application - * classes inherit. - */ -class ApplicationContext extends Context { - private final static String TAG = "ApplicationContext"; - private final static boolean DEBUG = false; - private final static boolean DEBUG_ICONS = false; - - private static final Object sSync = new Object(); - private static AlarmManager sAlarmManager; - private static PowerManager sPowerManager; - private static ConnectivityManager sConnectivityManager; - private static WifiManager sWifiManager; - private static LocationManager sLocationManager; - private static final HashMap sSharedPrefs = - new HashMap(); - - private AudioManager mAudioManager; - /*package*/ ActivityThread.PackageInfo mPackageInfo; - private Resources mResources; - /*package*/ ActivityThread mMainThread; - private Context mOuterContext; - private IBinder mActivityToken = null; - private ApplicationContentResolver mContentResolver; - private int mThemeResource = 0; - private Resources.Theme mTheme = null; - private PackageManager mPackageManager; - private NotificationManager mNotificationManager = null; - private ActivityManager mActivityManager = null; - private WallpaperManager mWallpaperManager = null; - private Context mReceiverRestrictedContext = null; - private SearchManager mSearchManager = null; - private SensorManager mSensorManager = null; - private Vibrator mVibrator = null; - private LayoutInflater mLayoutInflater = null; - private StatusBarManager mStatusBarManager = null; - private TelephonyManager mTelephonyManager = null; - private ClipboardManager mClipboardManager = null; - private boolean mRestricted; - private AccountManager mAccountManager; // protected by mSync - - private final Object mSync = new Object(); - - private File mDatabasesDir; - private File mPreferencesDir; - private File mFilesDir; - - - private File mCacheDir; - - private static long sInstanceCount = 0; - - private static final String[] EMPTY_FILE_LIST = {}; - - @Override - protected void finalize() throws Throwable { - super.finalize(); - --sInstanceCount; - } - - public static long getInstanceCount() { - return sInstanceCount; - } - - @Override - public AssetManager getAssets() { - return mResources.getAssets(); - } - - @Override - public Resources getResources() { - return mResources; - } - - @Override - public PackageManager getPackageManager() { - if (mPackageManager != null) { - return mPackageManager; - } - - IPackageManager pm = ActivityThread.getPackageManager(); - if (pm != null) { - // Doesn't matter if we make more than one instance. - return (mPackageManager = new ApplicationPackageManager(this, pm)); - } - - return null; - } - - @Override - public ContentResolver getContentResolver() { - return mContentResolver; - } - - @Override - public Looper getMainLooper() { - return mMainThread.getLooper(); - } - - @Override - public Context getApplicationContext() { - return mMainThread.getApplication(); - } - - @Override - public void setTheme(int resid) { - mThemeResource = resid; - } - - @Override - public Resources.Theme getTheme() { - if (mTheme == null) { - if (mThemeResource == 0) { - mThemeResource = com.android.internal.R.style.Theme; - } - mTheme = mResources.newTheme(); - mTheme.applyStyle(mThemeResource, true); - } - return mTheme; - } - - @Override - public ClassLoader getClassLoader() { - return mPackageInfo != null ? - mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader(); - } - - @Override - public String getPackageName() { - if (mPackageInfo != null) { - return mPackageInfo.getPackageName(); - } - throw new RuntimeException("Not supported in system context"); - } - - @Override - public ApplicationInfo getApplicationInfo() { - if (mPackageInfo != null) { - return mPackageInfo.getApplicationInfo(); - } - throw new RuntimeException("Not supported in system context"); - } - - @Override - public String getPackageResourcePath() { - if (mPackageInfo != null) { - return mPackageInfo.getResDir(); - } - throw new RuntimeException("Not supported in system context"); - } - - @Override - public String getPackageCodePath() { - if (mPackageInfo != null) { - return mPackageInfo.getAppDir(); - } - throw new RuntimeException("Not supported in system context"); - } - - private static File makeBackupFile(File prefsFile) { - return new File(prefsFile.getPath() + ".bak"); - } - - public File getSharedPrefsFile(String name) { - return makeFilename(getPreferencesDir(), name + ".xml"); - } - - @Override - public SharedPreferences getSharedPreferences(String name, int mode) { - SharedPreferencesImpl sp; - File f = getSharedPrefsFile(name); - synchronized (sSharedPrefs) { - sp = sSharedPrefs.get(f); - if (sp != null && !sp.hasFileChanged()) { - //Log.i(TAG, "Returning existing prefs " + name + ": " + sp); - return sp; - } - } - - FileInputStream str = null; - File backup = makeBackupFile(f); - if (backup.exists()) { - f.delete(); - backup.renameTo(f); - } - - // Debugging - if (f.exists() && !f.canRead()) { - Log.w(TAG, "Attempt to read preferences file " + f + " without permission"); - } - - Map map = null; - if (f.exists() && f.canRead()) { - try { - str = new FileInputStream(f); - map = XmlUtils.readMapXml(str); - str.close(); - } catch (org.xmlpull.v1.XmlPullParserException e) { - Log.w(TAG, "getSharedPreferences", e); - } catch (FileNotFoundException e) { - Log.w(TAG, "getSharedPreferences", e); - } catch (IOException e) { - Log.w(TAG, "getSharedPreferences", e); - } - } - - synchronized (sSharedPrefs) { - if (sp != null) { - //Log.i(TAG, "Updating existing prefs " + name + " " + sp + ": " + map); - sp.replace(map); - } else { - sp = sSharedPrefs.get(f); - if (sp == null) { - sp = new SharedPreferencesImpl(f, mode, map); - sSharedPrefs.put(f, sp); - } - } - return sp; - } - } - - private File getPreferencesDir() { - synchronized (mSync) { - if (mPreferencesDir == null) { - mPreferencesDir = new File(getDataDirFile(), "shared_prefs"); - } - return mPreferencesDir; - } - } - - @Override - public FileInputStream openFileInput(String name) - throws FileNotFoundException { - File f = makeFilename(getFilesDir(), name); - return new FileInputStream(f); - } - - @Override - public FileOutputStream openFileOutput(String name, int mode) - throws FileNotFoundException { - final boolean append = (mode&MODE_APPEND) != 0; - File f = makeFilename(getFilesDir(), name); - try { - FileOutputStream fos = new FileOutputStream(f, append); - setFilePermissionsFromMode(f.getPath(), mode, 0); - return fos; - } catch (FileNotFoundException e) { - } - - File parent = f.getParentFile(); - parent.mkdir(); - FileUtils.setPermissions( - parent.getPath(), - FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, - -1, -1); - FileOutputStream fos = new FileOutputStream(f, append); - setFilePermissionsFromMode(f.getPath(), mode, 0); - return fos; - } - - @Override - public boolean deleteFile(String name) { - File f = makeFilename(getFilesDir(), name); - return f.delete(); - } - - @Override - public File getFilesDir() { - synchronized (mSync) { - if (mFilesDir == null) { - mFilesDir = new File(getDataDirFile(), "files"); - } - if (!mFilesDir.exists()) { - if(!mFilesDir.mkdirs()) { - Log.w(TAG, "Unable to create files directory"); - return null; - } - FileUtils.setPermissions( - mFilesDir.getPath(), - FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, - -1, -1); - } - return mFilesDir; - } - } - - @Override - public File getCacheDir() { - synchronized (mSync) { - if (mCacheDir == null) { - mCacheDir = new File(getDataDirFile(), "cache"); - } - if (!mCacheDir.exists()) { - if(!mCacheDir.mkdirs()) { - Log.w(TAG, "Unable to create cache directory"); - return null; - } - FileUtils.setPermissions( - mCacheDir.getPath(), - FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, - -1, -1); - } - } - return mCacheDir; - } - - - @Override - public File getFileStreamPath(String name) { - return makeFilename(getFilesDir(), name); - } - - @Override - public String[] fileList() { - final String[] list = getFilesDir().list(); - return (list != null) ? list : EMPTY_FILE_LIST; - } - - @Override - public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) { - File dir = getDatabasesDir(); - if (!dir.isDirectory() && dir.mkdir()) { - FileUtils.setPermissions(dir.getPath(), - FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, - -1, -1); - } - - File f = makeFilename(dir, name); - SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f, factory); - setFilePermissionsFromMode(f.getPath(), mode, 0); - return db; - } - - @Override - public boolean deleteDatabase(String name) { - try { - File f = makeFilename(getDatabasesDir(), name); - return f.delete(); - } catch (Exception e) { - } - return false; - } - - @Override - public File getDatabasePath(String name) { - return makeFilename(getDatabasesDir(), name); - } - - @Override - public String[] databaseList() { - final String[] list = getDatabasesDir().list(); - return (list != null) ? list : EMPTY_FILE_LIST; - } - - - private File getDatabasesDir() { - synchronized (mSync) { - if (mDatabasesDir == null) { - mDatabasesDir = new File(getDataDirFile(), "databases"); - } - if (mDatabasesDir.getPath().equals("databases")) { - mDatabasesDir = new File("/data/system"); - } - return mDatabasesDir; - } - } - - @Override - public Drawable getWallpaper() { - return getWallpaperManager().getDrawable(); - } - - @Override - public Drawable peekWallpaper() { - return getWallpaperManager().peekDrawable(); - } - - @Override - public int getWallpaperDesiredMinimumWidth() { - return getWallpaperManager().getDesiredMinimumWidth(); - } - - @Override - public int getWallpaperDesiredMinimumHeight() { - return getWallpaperManager().getDesiredMinimumHeight(); - } - - @Override - public void setWallpaper(Bitmap bitmap) throws IOException { - getWallpaperManager().setBitmap(bitmap); - } - - @Override - public void setWallpaper(InputStream data) throws IOException { - getWallpaperManager().setStream(data); - } - - @Override - public void clearWallpaper() throws IOException { - getWallpaperManager().clear(); - } - - @Override - public void startActivity(Intent intent) { - if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { - throw new AndroidRuntimeException( - "Calling startActivity() from outside of an Activity " - + " context requires the FLAG_ACTIVITY_NEW_TASK flag." - + " Is this really what you want?"); - } - mMainThread.getInstrumentation().execStartActivity( - getOuterContext(), mMainThread.getApplicationThread(), null, null, intent, -1); - } - - @Override - public void startIntentSender(IntentSender intent, - Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) - throws IntentSender.SendIntentException { - try { - String resolvedType = null; - if (fillInIntent != null) { - resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); - } - int result = ActivityManagerNative.getDefault() - .startActivityIntentSender(mMainThread.getApplicationThread(), intent, - fillInIntent, resolvedType, null, null, - 0, flagsMask, flagsValues); - if (result == IActivityManager.START_CANCELED) { - throw new IntentSender.SendIntentException(); - } - Instrumentation.checkStartActivityResult(result, null); - } catch (RemoteException e) { - } - } - - @Override - public void sendBroadcast(Intent intent) { - String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); - try { - ActivityManagerNative.getDefault().broadcastIntent( - mMainThread.getApplicationThread(), intent, resolvedType, null, - Activity.RESULT_OK, null, null, null, false, false); - } catch (RemoteException e) { - } - } - - @Override - public void sendBroadcast(Intent intent, String receiverPermission) { - String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); - try { - ActivityManagerNative.getDefault().broadcastIntent( - mMainThread.getApplicationThread(), intent, resolvedType, null, - Activity.RESULT_OK, null, null, receiverPermission, false, false); - } catch (RemoteException e) { - } - } - - @Override - public void sendOrderedBroadcast(Intent intent, - String receiverPermission) { - String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); - try { - ActivityManagerNative.getDefault().broadcastIntent( - mMainThread.getApplicationThread(), intent, resolvedType, null, - Activity.RESULT_OK, null, null, receiverPermission, true, false); - } catch (RemoteException e) { - } - } - - @Override - public void sendOrderedBroadcast(Intent intent, - String receiverPermission, BroadcastReceiver resultReceiver, - Handler scheduler, int initialCode, String initialData, - Bundle initialExtras) { - IIntentReceiver rd = null; - if (resultReceiver != null) { - if (mPackageInfo != null) { - if (scheduler == null) { - scheduler = mMainThread.getHandler(); - } - rd = mPackageInfo.getReceiverDispatcher( - resultReceiver, getOuterContext(), scheduler, - mMainThread.getInstrumentation(), false); - } else { - if (scheduler == null) { - scheduler = mMainThread.getHandler(); - } - rd = new ActivityThread.PackageInfo.ReceiverDispatcher( - resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); - } - } - String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); - try { - ActivityManagerNative.getDefault().broadcastIntent( - mMainThread.getApplicationThread(), intent, resolvedType, rd, - initialCode, initialData, initialExtras, receiverPermission, - true, false); - } catch (RemoteException e) { - } - } - - @Override - public void sendStickyBroadcast(Intent intent) { - String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); - try { - ActivityManagerNative.getDefault().broadcastIntent( - mMainThread.getApplicationThread(), intent, resolvedType, null, - Activity.RESULT_OK, null, null, null, false, true); - } catch (RemoteException e) { - } - } - - @Override - public void sendStickyOrderedBroadcast(Intent intent, - BroadcastReceiver resultReceiver, - Handler scheduler, int initialCode, String initialData, - Bundle initialExtras) { - IIntentReceiver rd = null; - if (resultReceiver != null) { - if (mPackageInfo != null) { - if (scheduler == null) { - scheduler = mMainThread.getHandler(); - } - rd = mPackageInfo.getReceiverDispatcher( - resultReceiver, getOuterContext(), scheduler, - mMainThread.getInstrumentation(), false); - } else { - if (scheduler == null) { - scheduler = mMainThread.getHandler(); - } - rd = new ActivityThread.PackageInfo.ReceiverDispatcher( - resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); - } - } - String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); - try { - ActivityManagerNative.getDefault().broadcastIntent( - mMainThread.getApplicationThread(), intent, resolvedType, rd, - initialCode, initialData, initialExtras, null, - true, true); - } catch (RemoteException e) { - } - } - - @Override - public void removeStickyBroadcast(Intent intent) { - String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); - if (resolvedType != null) { - intent = new Intent(intent); - intent.setDataAndType(intent.getData(), resolvedType); - } - try { - ActivityManagerNative.getDefault().unbroadcastIntent( - mMainThread.getApplicationThread(), intent); - } catch (RemoteException e) { - } - } - - @Override - public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { - return registerReceiver(receiver, filter, null, null); - } - - @Override - public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, - String broadcastPermission, Handler scheduler) { - return registerReceiverInternal(receiver, filter, broadcastPermission, - scheduler, getOuterContext()); - } - - private Intent registerReceiverInternal(BroadcastReceiver receiver, - IntentFilter filter, String broadcastPermission, - Handler scheduler, Context context) { - IIntentReceiver rd = null; - if (receiver != null) { - if (mPackageInfo != null && context != null) { - if (scheduler == null) { - scheduler = mMainThread.getHandler(); - } - rd = mPackageInfo.getReceiverDispatcher( - receiver, context, scheduler, - mMainThread.getInstrumentation(), true); - } else { - if (scheduler == null) { - scheduler = mMainThread.getHandler(); - } - rd = new ActivityThread.PackageInfo.ReceiverDispatcher( - receiver, context, scheduler, null, false).getIIntentReceiver(); - } - } - try { - return ActivityManagerNative.getDefault().registerReceiver( - mMainThread.getApplicationThread(), - rd, filter, broadcastPermission); - } catch (RemoteException e) { - return null; - } - } - - @Override - public void unregisterReceiver(BroadcastReceiver receiver) { - if (mPackageInfo != null) { - IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher( - getOuterContext(), receiver); - try { - ActivityManagerNative.getDefault().unregisterReceiver(rd); - } catch (RemoteException e) { - } - } else { - throw new RuntimeException("Not supported in system context"); - } - } - - @Override - public ComponentName startService(Intent service) { - try { - ComponentName cn = ActivityManagerNative.getDefault().startService( - mMainThread.getApplicationThread(), service, - service.resolveTypeIfNeeded(getContentResolver())); - if (cn != null && cn.getPackageName().equals("!")) { - throw new SecurityException( - "Not allowed to start service " + service - + " without permission " + cn.getClassName()); - } - return cn; - } catch (RemoteException e) { - return null; - } - } - - @Override - public boolean stopService(Intent service) { - try { - int res = ActivityManagerNative.getDefault().stopService( - mMainThread.getApplicationThread(), service, - service.resolveTypeIfNeeded(getContentResolver())); - if (res < 0) { - throw new SecurityException( - "Not allowed to stop service " + service); - } - return res != 0; - } catch (RemoteException e) { - return false; - } - } - - @Override - public boolean bindService(Intent service, ServiceConnection conn, - int flags) { - IServiceConnection sd; - if (mPackageInfo != null) { - sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), - mMainThread.getHandler(), flags); - } else { - throw new RuntimeException("Not supported in system context"); - } - try { - int res = ActivityManagerNative.getDefault().bindService( - mMainThread.getApplicationThread(), getActivityToken(), - service, service.resolveTypeIfNeeded(getContentResolver()), - sd, flags); - if (res < 0) { - throw new SecurityException( - "Not allowed to bind to service " + service); - } - return res != 0; - } catch (RemoteException e) { - return false; - } - } - - @Override - public void unbindService(ServiceConnection conn) { - if (mPackageInfo != null) { - IServiceConnection sd = mPackageInfo.forgetServiceDispatcher( - getOuterContext(), conn); - try { - ActivityManagerNative.getDefault().unbindService(sd); - } catch (RemoteException e) { - } - } else { - throw new RuntimeException("Not supported in system context"); - } - } - - @Override - public boolean startInstrumentation(ComponentName className, - String profileFile, Bundle arguments) { - try { - return ActivityManagerNative.getDefault().startInstrumentation( - className, profileFile, 0, arguments, null); - } catch (RemoteException e) { - // System has crashed, nothing we can do. - } - return false; - } - - @Override - public Object getSystemService(String name) { - if (WINDOW_SERVICE.equals(name)) { - return WindowManagerImpl.getDefault(); - } else if (LAYOUT_INFLATER_SERVICE.equals(name)) { - synchronized (mSync) { - LayoutInflater inflater = mLayoutInflater; - if (inflater != null) { - return inflater; - } - mLayoutInflater = inflater = - PolicyManager.makeNewLayoutInflater(getOuterContext()); - return inflater; - } - } else if (ACTIVITY_SERVICE.equals(name)) { - return getActivityManager(); - } else if (INPUT_METHOD_SERVICE.equals(name)) { - return InputMethodManager.getInstance(this); - } else if (ALARM_SERVICE.equals(name)) { - return getAlarmManager(); - } else if (ACCOUNT_SERVICE.equals(name)) { - return getAccountManager(); - } else if (POWER_SERVICE.equals(name)) { - return getPowerManager(); - } else if (CONNECTIVITY_SERVICE.equals(name)) { - return getConnectivityManager(); - } else if (WIFI_SERVICE.equals(name)) { - return getWifiManager(); - } else if (NOTIFICATION_SERVICE.equals(name)) { - return getNotificationManager(); - } else if (KEYGUARD_SERVICE.equals(name)) { - return new KeyguardManager(); - } else if (ACCESSIBILITY_SERVICE.equals(name)) { - return AccessibilityManager.getInstance(this); - } else if (LOCATION_SERVICE.equals(name)) { - return getLocationManager(); - } else if (SEARCH_SERVICE.equals(name)) { - return getSearchManager(); - } else if (SENSOR_SERVICE.equals(name)) { - return getSensorManager(); - } else if (VIBRATOR_SERVICE.equals(name)) { - return getVibrator(); - } else if (STATUS_BAR_SERVICE.equals(name)) { - synchronized (mSync) { - if (mStatusBarManager == null) { - mStatusBarManager = new StatusBarManager(getOuterContext()); - } - return mStatusBarManager; - } - } else if (AUDIO_SERVICE.equals(name)) { - return getAudioManager(); - } else if (TELEPHONY_SERVICE.equals(name)) { - return getTelephonyManager(); - } else if (CLIPBOARD_SERVICE.equals(name)) { - return getClipboardManager(); - } else if (WALLPAPER_SERVICE.equals(name)) { - return getWallpaperManager(); - } - - return null; - } - - private AccountManager getAccountManager() { - synchronized (mSync) { - if (mAccountManager == null) { - IBinder b = ServiceManager.getService(ACCOUNT_SERVICE); - IAccountManager service = IAccountManager.Stub.asInterface(b); - mAccountManager = new AccountManager(this, service); - } - return mAccountManager; - } - } - - private ActivityManager getActivityManager() { - synchronized (mSync) { - if (mActivityManager == null) { - mActivityManager = new ActivityManager(getOuterContext(), - mMainThread.getHandler()); - } - } - return mActivityManager; - } - - private AlarmManager getAlarmManager() { - synchronized (sSync) { - if (sAlarmManager == null) { - IBinder b = ServiceManager.getService(ALARM_SERVICE); - IAlarmManager service = IAlarmManager.Stub.asInterface(b); - sAlarmManager = new AlarmManager(service); - } - } - return sAlarmManager; - } - - private PowerManager getPowerManager() { - synchronized (sSync) { - if (sPowerManager == null) { - IBinder b = ServiceManager.getService(POWER_SERVICE); - IPowerManager service = IPowerManager.Stub.asInterface(b); - sPowerManager = new PowerManager(service, mMainThread.getHandler()); - } - } - return sPowerManager; - } - - private ConnectivityManager getConnectivityManager() - { - synchronized (sSync) { - if (sConnectivityManager == null) { - IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE); - IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); - sConnectivityManager = new ConnectivityManager(service); - } - } - return sConnectivityManager; - } - - private WifiManager getWifiManager() - { - synchronized (sSync) { - if (sWifiManager == null) { - IBinder b = ServiceManager.getService(WIFI_SERVICE); - IWifiManager service = IWifiManager.Stub.asInterface(b); - sWifiManager = new WifiManager(service, mMainThread.getHandler()); - } - } - return sWifiManager; - } - - private NotificationManager getNotificationManager() { - synchronized (mSync) { - if (mNotificationManager == null) { - mNotificationManager = new NotificationManager( - new ContextThemeWrapper(getOuterContext(), com.android.internal.R.style.Theme_Dialog), - mMainThread.getHandler()); - } - } - return mNotificationManager; - } - - private WallpaperManager getWallpaperManager() { - synchronized (mSync) { - if (mWallpaperManager == null) { - mWallpaperManager = new WallpaperManager(getOuterContext(), - mMainThread.getHandler()); - } - } - return mWallpaperManager; - } - - private TelephonyManager getTelephonyManager() { - synchronized (mSync) { - if (mTelephonyManager == null) { - mTelephonyManager = new TelephonyManager(getOuterContext()); - } - } - return mTelephonyManager; - } - - private ClipboardManager getClipboardManager() { - synchronized (mSync) { - if (mClipboardManager == null) { - mClipboardManager = new ClipboardManager(getOuterContext(), - mMainThread.getHandler()); - } - } - return mClipboardManager; - } - - private LocationManager getLocationManager() { - synchronized (sSync) { - if (sLocationManager == null) { - IBinder b = ServiceManager.getService(LOCATION_SERVICE); - ILocationManager service = ILocationManager.Stub.asInterface(b); - sLocationManager = new LocationManager(service); - } - } - return sLocationManager; - } - - private SearchManager getSearchManager() { - synchronized (mSync) { - if (mSearchManager == null) { - mSearchManager = new SearchManager(getOuterContext(), mMainThread.getHandler()); - } - } - return mSearchManager; - } - - private SensorManager getSensorManager() { - synchronized (mSync) { - if (mSensorManager == null) { - mSensorManager = new SensorManager(mMainThread.getHandler().getLooper()); - } - } - return mSensorManager; - } - - private Vibrator getVibrator() { - synchronized (mSync) { - if (mVibrator == null) { - mVibrator = new Vibrator(); - } - } - return mVibrator; - } - - private AudioManager getAudioManager() - { - if (mAudioManager == null) { - mAudioManager = new AudioManager(this); - } - return mAudioManager; - } - - @Override - public int checkPermission(String permission, int pid, int uid) { - if (permission == null) { - throw new IllegalArgumentException("permission is null"); - } - - if (!Process.supportsProcesses()) { - return PackageManager.PERMISSION_GRANTED; - } - try { - return ActivityManagerNative.getDefault().checkPermission( - permission, pid, uid); - } catch (RemoteException e) { - return PackageManager.PERMISSION_DENIED; - } - } - - @Override - public int checkCallingPermission(String permission) { - if (permission == null) { - throw new IllegalArgumentException("permission is null"); - } - - if (!Process.supportsProcesses()) { - return PackageManager.PERMISSION_GRANTED; - } - int pid = Binder.getCallingPid(); - if (pid != Process.myPid()) { - return checkPermission(permission, pid, - Binder.getCallingUid()); - } - return PackageManager.PERMISSION_DENIED; - } - - @Override - public int checkCallingOrSelfPermission(String permission) { - if (permission == null) { - throw new IllegalArgumentException("permission is null"); - } - - return checkPermission(permission, Binder.getCallingPid(), - Binder.getCallingUid()); - } - - private void enforce( - String permission, int resultOfCheck, - boolean selfToo, int uid, String message) { - if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { - throw new SecurityException( - (message != null ? (message + ": ") : "") + - (selfToo - ? "Neither user " + uid + " nor current process has " - : "User " + uid + " does not have ") + - permission + - "."); - } - } - - public void enforcePermission( - String permission, int pid, int uid, String message) { - enforce(permission, - checkPermission(permission, pid, uid), - false, - uid, - message); - } - - public void enforceCallingPermission(String permission, String message) { - enforce(permission, - checkCallingPermission(permission), - false, - Binder.getCallingUid(), - message); - } - - public void enforceCallingOrSelfPermission( - String permission, String message) { - enforce(permission, - checkCallingOrSelfPermission(permission), - true, - Binder.getCallingUid(), - message); - } - - @Override - public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { - try { - ActivityManagerNative.getDefault().grantUriPermission( - mMainThread.getApplicationThread(), toPackage, uri, - modeFlags); - } catch (RemoteException e) { - } - } - - @Override - public void revokeUriPermission(Uri uri, int modeFlags) { - try { - ActivityManagerNative.getDefault().revokeUriPermission( - mMainThread.getApplicationThread(), uri, - modeFlags); - } catch (RemoteException e) { - } - } - - @Override - public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { - if (!Process.supportsProcesses()) { - return PackageManager.PERMISSION_GRANTED; - } - try { - return ActivityManagerNative.getDefault().checkUriPermission( - uri, pid, uid, modeFlags); - } catch (RemoteException e) { - return PackageManager.PERMISSION_DENIED; - } - } - - @Override - public int checkCallingUriPermission(Uri uri, int modeFlags) { - if (!Process.supportsProcesses()) { - return PackageManager.PERMISSION_GRANTED; - } - int pid = Binder.getCallingPid(); - if (pid != Process.myPid()) { - return checkUriPermission(uri, pid, - Binder.getCallingUid(), modeFlags); - } - return PackageManager.PERMISSION_DENIED; - } - - @Override - public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { - return checkUriPermission(uri, Binder.getCallingPid(), - Binder.getCallingUid(), modeFlags); - } - - @Override - public int checkUriPermission(Uri uri, String readPermission, - String writePermission, int pid, int uid, int modeFlags) { - if (DEBUG) { - Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission=" - + readPermission + " writePermission=" + writePermission - + " pid=" + pid + " uid=" + uid + " mode" + modeFlags); - } - if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { - if (readPermission == null - || checkPermission(readPermission, pid, uid) - == PackageManager.PERMISSION_GRANTED) { - return PackageManager.PERMISSION_GRANTED; - } - } - if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { - if (writePermission == null - || checkPermission(writePermission, pid, uid) - == PackageManager.PERMISSION_GRANTED) { - return PackageManager.PERMISSION_GRANTED; - } - } - return uri != null ? checkUriPermission(uri, pid, uid, modeFlags) - : PackageManager.PERMISSION_DENIED; - } - - private String uriModeFlagToString(int uriModeFlags) { - switch (uriModeFlags) { - case Intent.FLAG_GRANT_READ_URI_PERMISSION | - Intent.FLAG_GRANT_WRITE_URI_PERMISSION: - return "read and write"; - case Intent.FLAG_GRANT_READ_URI_PERMISSION: - return "read"; - case Intent.FLAG_GRANT_WRITE_URI_PERMISSION: - return "write"; - } - throw new IllegalArgumentException( - "Unknown permission mode flags: " + uriModeFlags); - } - - private void enforceForUri( - int modeFlags, int resultOfCheck, boolean selfToo, - int uid, Uri uri, String message) { - if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { - throw new SecurityException( - (message != null ? (message + ": ") : "") + - (selfToo - ? "Neither user " + uid + " nor current process has " - : "User " + uid + " does not have ") + - uriModeFlagToString(modeFlags) + - " permission on " + - uri + - "."); - } - } - - public void enforceUriPermission( - Uri uri, int pid, int uid, int modeFlags, String message) { - enforceForUri( - modeFlags, checkUriPermission(uri, pid, uid, modeFlags), - false, uid, uri, message); - } - - public void enforceCallingUriPermission( - Uri uri, int modeFlags, String message) { - enforceForUri( - modeFlags, checkCallingUriPermission(uri, modeFlags), - false, Binder.getCallingUid(), uri, message); - } - - public void enforceCallingOrSelfUriPermission( - Uri uri, int modeFlags, String message) { - enforceForUri( - modeFlags, - checkCallingOrSelfUriPermission(uri, modeFlags), true, - Binder.getCallingUid(), uri, message); - } - - public void enforceUriPermission( - Uri uri, String readPermission, String writePermission, - int pid, int uid, int modeFlags, String message) { - enforceForUri(modeFlags, - checkUriPermission( - uri, readPermission, writePermission, pid, uid, - modeFlags), - false, - uid, - uri, - message); - } - - @Override - public Context createPackageContext(String packageName, int flags) - throws PackageManager.NameNotFoundException { - if (packageName.equals("system") || packageName.equals("android")) { - return new ApplicationContext(mMainThread.getSystemContext()); - } - - ActivityThread.PackageInfo pi = - mMainThread.getPackageInfo(packageName, flags); - if (pi != null) { - ApplicationContext c = new ApplicationContext(); - c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED; - c.init(pi, null, mMainThread, mResources); - if (c.mResources != null) { - return c; - } - } - - // Should be a better exception. - throw new PackageManager.NameNotFoundException( - "Application package " + packageName + " not found"); - } - - @Override - public boolean isRestricted() { - return mRestricted; - } - - private File getDataDirFile() { - if (mPackageInfo != null) { - return mPackageInfo.getDataDirFile(); - } - throw new RuntimeException("Not supported in system context"); - } - - @Override - public File getDir(String name, int mode) { - name = "app_" + name; - File file = makeFilename(getDataDirFile(), name); - if (!file.exists()) { - file.mkdir(); - setFilePermissionsFromMode(file.getPath(), mode, - FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH); - } - return file; - } - - static ApplicationContext createSystemContext(ActivityThread mainThread) { - ApplicationContext context = new ApplicationContext(); - context.init(Resources.getSystem(), mainThread); - return context; - } - - ApplicationContext() { - ++sInstanceCount; - mOuterContext = this; - } - - /** - * Create a new ApplicationContext from an existing one. The new one - * works and operates the same as the one it is copying. - * - * @param context Existing application context. - */ - public ApplicationContext(ApplicationContext context) { - ++sInstanceCount; - mPackageInfo = context.mPackageInfo; - mResources = context.mResources; - mMainThread = context.mMainThread; - mContentResolver = context.mContentResolver; - mOuterContext = this; - } - - final void init(ActivityThread.PackageInfo packageInfo, - IBinder activityToken, ActivityThread mainThread) { - init(packageInfo, activityToken, mainThread, null); - } - - final void init(ActivityThread.PackageInfo packageInfo, - IBinder activityToken, ActivityThread mainThread, - Resources container) { - mPackageInfo = packageInfo; - mResources = mPackageInfo.getResources(mainThread); - - if (container != null && container.getCompatibilityInfo().applicationScale != - mResources.getCompatibilityInfo().applicationScale) { - if (DEBUG) { - Log.d(TAG, "loaded context has different scaling. Using container's" + - " compatiblity info:" + container.getDisplayMetrics()); - } - mResources = mainThread.getTopLevelResources( - mPackageInfo.getResDir(), container.getCompatibilityInfo().copy()); - } - mMainThread = mainThread; - mContentResolver = new ApplicationContentResolver(this, mainThread); - - setActivityToken(activityToken); - } - - final void init(Resources resources, ActivityThread mainThread) { - mPackageInfo = null; - mResources = resources; - mMainThread = mainThread; - mContentResolver = new ApplicationContentResolver(this, mainThread); - } - - final void scheduleFinalCleanup(String who, String what) { - mMainThread.scheduleContextCleanup(this, who, what); - } - - final void performFinalCleanup(String who, String what) { - //Log.i(TAG, "Cleanup up context: " + this); - mPackageInfo.removeContextRegistrations(getOuterContext(), who, what); - } - - final Context getReceiverRestrictedContext() { - if (mReceiverRestrictedContext != null) { - return mReceiverRestrictedContext; - } - return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext()); - } - - final void setActivityToken(IBinder token) { - mActivityToken = token; - } - - final void setOuterContext(Context context) { - mOuterContext = context; - } - - final Context getOuterContext() { - return mOuterContext; - } - - final IBinder getActivityToken() { - return mActivityToken; - } - - private static void setFilePermissionsFromMode(String name, int mode, - int extraPermissions) { - int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR - |FileUtils.S_IRGRP|FileUtils.S_IWGRP - |extraPermissions; - if ((mode&MODE_WORLD_READABLE) != 0) { - perms |= FileUtils.S_IROTH; - } - if ((mode&MODE_WORLD_WRITEABLE) != 0) { - perms |= FileUtils.S_IWOTH; - } - if (DEBUG) { - Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode) - + ", perms=0x" + Integer.toHexString(perms)); - } - FileUtils.setPermissions(name, perms, -1, -1); - } - - private File makeFilename(File base, String name) { - if (name.indexOf(File.separatorChar) < 0) { - return new File(base, name); - } - throw new IllegalArgumentException( - "File " + name + " contains a path separator"); - } - - // ---------------------------------------------------------------------- - // ---------------------------------------------------------------------- - // ---------------------------------------------------------------------- - - private static final class ApplicationContentResolver extends ContentResolver { - public ApplicationContentResolver(Context context, - ActivityThread mainThread) - { - super(context); - mMainThread = mainThread; - } - - @Override - protected IContentProvider acquireProvider(Context context, String name) - { - return mMainThread.acquireProvider(context, name); - } - - @Override - public boolean releaseProvider(IContentProvider provider) - { - return mMainThread.releaseProvider(provider); - } - - private final ActivityThread mMainThread; - } - - // ---------------------------------------------------------------------- - // ---------------------------------------------------------------------- - // ---------------------------------------------------------------------- - - /*package*/ - static final class ApplicationPackageManager extends PackageManager { - @Override - public PackageInfo getPackageInfo(String packageName, int flags) - throws NameNotFoundException { - try { - PackageInfo pi = mPM.getPackageInfo(packageName, flags); - if (pi != null) { - return pi; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - - throw new NameNotFoundException(packageName); - } - - @Override - public Intent getLaunchIntentForPackage(String packageName) { - // First see if the package has an INFO activity; the existence of - // such an activity is implied to be the desired front-door for the - // overall package (such as if it has multiple launcher entries). - Intent intentToResolve = new Intent(Intent.ACTION_MAIN); - intentToResolve.addCategory(Intent.CATEGORY_INFO); - intentToResolve.setPackage(packageName); - ResolveInfo resolveInfo = resolveActivity(intentToResolve, 0); - - // Otherwise, try to find a main launcher activity. - if (resolveInfo == null) { - // reuse the intent instance - intentToResolve.removeCategory(Intent.CATEGORY_INFO); - intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER); - intentToResolve.setPackage(packageName); - resolveInfo = resolveActivity(intentToResolve, 0); - } - if (resolveInfo == null) { - return null; - } - Intent intent = new Intent(Intent.ACTION_MAIN); - intent.setClassName(packageName, resolveInfo.activityInfo.name); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - return intent; - } - - @Override - public int[] getPackageGids(String packageName) - throws NameNotFoundException { - try { - int[] gids = mPM.getPackageGids(packageName); - if (gids == null || gids.length > 0) { - return gids; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - - throw new NameNotFoundException(packageName); - } - - @Override - public PermissionInfo getPermissionInfo(String name, int flags) - throws NameNotFoundException { - try { - PermissionInfo pi = mPM.getPermissionInfo(name, flags); - if (pi != null) { - return pi; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - - throw new NameNotFoundException(name); - } - - @Override - public List queryPermissionsByGroup(String group, int flags) - throws NameNotFoundException { - try { - List pi = mPM.queryPermissionsByGroup(group, flags); - if (pi != null) { - return pi; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - - throw new NameNotFoundException(group); - } - - @Override - public PermissionGroupInfo getPermissionGroupInfo(String name, - int flags) throws NameNotFoundException { - try { - PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags); - if (pgi != null) { - return pgi; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - - throw new NameNotFoundException(name); - } - - @Override - public List getAllPermissionGroups(int flags) { - try { - return mPM.getAllPermissionGroups(flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public ApplicationInfo getApplicationInfo(String packageName, int flags) - throws NameNotFoundException { - try { - ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags); - if (ai != null) { - return ai; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - - throw new NameNotFoundException(packageName); - } - - @Override - public ActivityInfo getActivityInfo(ComponentName className, int flags) - throws NameNotFoundException { - try { - ActivityInfo ai = mPM.getActivityInfo(className, flags); - if (ai != null) { - return ai; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - - throw new NameNotFoundException(className.toString()); - } - - @Override - public ActivityInfo getReceiverInfo(ComponentName className, int flags) - throws NameNotFoundException { - try { - ActivityInfo ai = mPM.getReceiverInfo(className, flags); - if (ai != null) { - return ai; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - - throw new NameNotFoundException(className.toString()); - } - - @Override - public ServiceInfo getServiceInfo(ComponentName className, int flags) - throws NameNotFoundException { - try { - ServiceInfo si = mPM.getServiceInfo(className, flags); - if (si != null) { - return si; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - - throw new NameNotFoundException(className.toString()); - } - - @Override - public String[] getSystemSharedLibraryNames() { - try { - return mPM.getSystemSharedLibraryNames(); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public FeatureInfo[] getSystemAvailableFeatures() { - try { - return mPM.getSystemAvailableFeatures(); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public boolean hasSystemFeature(String name) { - try { - return mPM.hasSystemFeature(name); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public int checkPermission(String permName, String pkgName) { - try { - return mPM.checkPermission(permName, pkgName); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public boolean addPermission(PermissionInfo info) { - try { - return mPM.addPermission(info); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public void removePermission(String name) { - try { - mPM.removePermission(name); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public int checkSignatures(String pkg1, String pkg2) { - try { - return mPM.checkSignatures(pkg1, pkg2); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public int checkSignatures(int uid1, int uid2) { - try { - return mPM.checkUidSignatures(uid1, uid2); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public String[] getPackagesForUid(int uid) { - try { - return mPM.getPackagesForUid(uid); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public String getNameForUid(int uid) { - try { - return mPM.getNameForUid(uid); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public int getUidForSharedUser(String sharedUserName) - throws NameNotFoundException { - try { - int uid = mPM.getUidForSharedUser(sharedUserName); - if(uid != -1) { - return uid; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - throw new NameNotFoundException("No shared userid for user:"+sharedUserName); - } - - @Override - public List getInstalledPackages(int flags) { - try { - return mPM.getInstalledPackages(flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public List getInstalledApplications(int flags) { - try { - return mPM.getInstalledApplications(flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public ResolveInfo resolveActivity(Intent intent, int flags) { - try { - return mPM.resolveIntent( - intent, - intent.resolveTypeIfNeeded(mContext.getContentResolver()), - flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public List queryIntentActivities(Intent intent, - int flags) { - try { - return mPM.queryIntentActivities( - intent, - intent.resolveTypeIfNeeded(mContext.getContentResolver()), - flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public List queryIntentActivityOptions( - ComponentName caller, Intent[] specifics, Intent intent, - int flags) { - final ContentResolver resolver = mContext.getContentResolver(); - - String[] specificTypes = null; - if (specifics != null) { - final int N = specifics.length; - for (int i=0; i queryBroadcastReceivers(Intent intent, int flags) { - try { - return mPM.queryIntentReceivers( - intent, - intent.resolveTypeIfNeeded(mContext.getContentResolver()), - flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public ResolveInfo resolveService(Intent intent, int flags) { - try { - return mPM.resolveService( - intent, - intent.resolveTypeIfNeeded(mContext.getContentResolver()), - flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public List queryIntentServices(Intent intent, int flags) { - try { - return mPM.queryIntentServices( - intent, - intent.resolveTypeIfNeeded(mContext.getContentResolver()), - flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public ProviderInfo resolveContentProvider(String name, - int flags) { - try { - return mPM.resolveContentProvider(name, flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public List queryContentProviders(String processName, - int uid, int flags) { - try { - return mPM.queryContentProviders(processName, uid, flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override - public InstrumentationInfo getInstrumentationInfo( - ComponentName className, int flags) - throws NameNotFoundException { - try { - InstrumentationInfo ii = mPM.getInstrumentationInfo( - className, flags); - if (ii != null) { - return ii; - } - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - - throw new NameNotFoundException(className.toString()); - } - - @Override - public List queryInstrumentation( - String targetPackage, int flags) { - try { - return mPM.queryInstrumentation(targetPackage, flags); - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - @Override public Drawable getDrawable(String packageName, int resid, - ApplicationInfo appInfo) { - ResourceName name = new ResourceName(packageName, resid); - Drawable dr = getCachedIcon(name); - if (dr != null) { - return dr; - } - if (appInfo == null) { - try { - appInfo = getApplicationInfo(packageName, 0); - } catch (NameNotFoundException e) { - return null; - } - } - try { - Resources r = getResourcesForApplication(appInfo); - dr = r.getDrawable(resid); - if (false) { - RuntimeException e = new RuntimeException("here"); - e.fillInStackTrace(); - Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resid) - + " from package " + packageName - + ": app scale=" + r.getCompatibilityInfo().applicationScale - + ", caller scale=" + mContext.getResources().getCompatibilityInfo().applicationScale, - e); - } - if (DEBUG_ICONS) Log.v(TAG, "Getting drawable 0x" - + Integer.toHexString(resid) + " from " + r - + ": " + dr); - putCachedIcon(name, dr); - return dr; - } catch (NameNotFoundException e) { - Log.w("PackageManager", "Failure retrieving resources for" - + appInfo.packageName); - } catch (RuntimeException e) { - // If an exception was thrown, fall through to return - // default icon. - Log.w("PackageManager", "Failure retrieving icon 0x" - + Integer.toHexString(resid) + " in package " - + packageName, e); - } - return null; - } - - @Override public Drawable getActivityIcon(ComponentName activityName) - throws NameNotFoundException { - return getActivityInfo(activityName, 0).loadIcon(this); - } - - @Override public Drawable getActivityIcon(Intent intent) - throws NameNotFoundException { - if (intent.getComponent() != null) { - return getActivityIcon(intent.getComponent()); - } - - ResolveInfo info = resolveActivity( - intent, PackageManager.MATCH_DEFAULT_ONLY); - if (info != null) { - return info.activityInfo.loadIcon(this); - } - - throw new NameNotFoundException(intent.toUri(0)); - } - - @Override public Drawable getDefaultActivityIcon() { - return Resources.getSystem().getDrawable( - com.android.internal.R.drawable.sym_def_app_icon); - } - - @Override public Drawable getApplicationIcon(ApplicationInfo info) { - final int icon = info.icon; - if (icon != 0) { - ResourceName name = new ResourceName(info, icon); - Drawable dr = getCachedIcon(name); - if (dr != null) { - return dr; - } - try { - Resources r = getResourcesForApplication(info); - dr = r.getDrawable(icon); - if (DEBUG_ICONS) Log.v(TAG, "Getting drawable 0x" - + Integer.toHexString(icon) + " from " + r - + ": " + dr); - putCachedIcon(name, dr); - return dr; - } catch (NameNotFoundException e) { - Log.w("PackageManager", "Failure retrieving resources for" - + info.packageName); - } catch (RuntimeException e) { - // If an exception was thrown, fall through to return - // default icon. - Log.w("PackageManager", "Failure retrieving app icon", e); - } - } - return getDefaultActivityIcon(); - } - - @Override public Drawable getApplicationIcon(String packageName) - throws NameNotFoundException { - return getApplicationIcon(getApplicationInfo(packageName, 0)); - } - - @Override public Resources getResourcesForActivity( - ComponentName activityName) throws NameNotFoundException { - return getResourcesForApplication( - getActivityInfo(activityName, 0).applicationInfo); - } - - @Override public Resources getResourcesForApplication( - ApplicationInfo app) throws NameNotFoundException { - if (app.packageName.equals("system")) { - return mContext.mMainThread.getSystemContext().getResources(); - } - Resources r = mContext.mMainThread.getTopLevelResources( - app.uid == Process.myUid() ? app.sourceDir - : app.publicSourceDir, mContext.mPackageInfo); - if (r != null) { - return r; - } - throw new NameNotFoundException("Unable to open " + app.publicSourceDir); - } - - @Override public Resources getResourcesForApplication( - String appPackageName) throws NameNotFoundException { - return getResourcesForApplication( - getApplicationInfo(appPackageName, 0)); - } - - int mCachedSafeMode = -1; - @Override public boolean isSafeMode() { - try { - if (mCachedSafeMode < 0) { - mCachedSafeMode = mPM.isSafeMode() ? 1 : 0; - } - return mCachedSafeMode != 0; - } catch (RemoteException e) { - throw new RuntimeException("Package manager has died", e); - } - } - - static void configurationChanged() { - synchronized (sSync) { - sIconCache.clear(); - sStringCache.clear(); - } - } - - ApplicationPackageManager(ApplicationContext context, - IPackageManager pm) { - mContext = context; - mPM = pm; - } - - private Drawable getCachedIcon(ResourceName name) { - synchronized (sSync) { - WeakReference wr = sIconCache.get(name); - if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for " - + name + ": " + wr); - if (wr != null) { // we have the activity - Drawable dr = wr.get(); - if (dr != null) { - if (DEBUG_ICONS) Log.v(TAG, "Get cached drawable for " - + name + ": " + dr); - return dr; - } - // our entry has been purged - sIconCache.remove(name); - } - } - return null; - } - - private void establishPackageRemovedReceiver() { - // mContext.registerReceiverInternal() winds up acquiring the - // main ActivityManagerService.this lock. If we hold our usual - // sSync global lock at the same time, we impose a required ordering - // on those two locks, which is not good for deadlock prevention. - // Use a dedicated lock around initialization of - // sPackageRemovedReceiver to avoid this. - synchronized (sPackageRemovedSync) { - if (sPackageRemovedReceiver == null) { - sPackageRemovedReceiver = new PackageRemovedReceiver(); - IntentFilter filter = new IntentFilter( - Intent.ACTION_PACKAGE_REMOVED); - filter.addDataScheme("package"); - mContext.registerReceiverInternal(sPackageRemovedReceiver, - filter, null, null, null); - } - } - } - - private void putCachedIcon(ResourceName name, Drawable dr) { - establishPackageRemovedReceiver(); - - synchronized (sSync) { - sIconCache.put(name, new WeakReference(dr)); - if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable for " - + name + ": " + dr); - } - } - - private static final class PackageRemovedReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - Uri data = intent.getData(); - String ssp; - if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { - boolean needCleanup = false; - synchronized (sSync) { - Iterator it = sIconCache.keySet().iterator(); - while (it.hasNext()) { - ResourceName nm = it.next(); - if (nm.packageName.equals(ssp)) { - //Log.i(TAG, "Removing cached drawable for " + nm); - it.remove(); - needCleanup = true; - } - } - it = sStringCache.keySet().iterator(); - while (it.hasNext()) { - ResourceName nm = it.next(); - if (nm.packageName.equals(ssp)) { - //Log.i(TAG, "Removing cached string for " + nm); - it.remove(); - needCleanup = true; - } - } - } - if (needCleanup || ActivityThread.currentActivityThread().hasPackageInfo(ssp)) { - ActivityThread.currentActivityThread().scheduleGcIdler(); - } - } - } - } - - private static final class ResourceName { - final String packageName; - final int iconId; - - ResourceName(String _packageName, int _iconId) { - packageName = _packageName; - iconId = _iconId; - } - - ResourceName(ApplicationInfo aInfo, int _iconId) { - this(aInfo.packageName, _iconId); - } - - ResourceName(ComponentInfo cInfo, int _iconId) { - this(cInfo.applicationInfo.packageName, _iconId); - } - - ResourceName(ResolveInfo rInfo, int _iconId) { - this(rInfo.activityInfo.applicationInfo.packageName, _iconId); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - ResourceName that = (ResourceName) o; - - if (iconId != that.iconId) return false; - return !(packageName != null ? - !packageName.equals(that.packageName) : that.packageName != null); - - } - - @Override - public int hashCode() { - int result; - result = packageName.hashCode(); - result = 31 * result + iconId; - return result; - } - - @Override - public String toString() { - return "{ResourceName " + packageName + " / " + iconId + "}"; - } - } - - private CharSequence getCachedString(ResourceName name) { - synchronized (sSync) { - WeakReference wr = sStringCache.get(name); - if (wr != null) { // we have the activity - CharSequence cs = wr.get(); - if (cs != null) { - return cs; - } - // our entry has been purged - sStringCache.remove(name); - } - } - return null; - } - - private void putCachedString(ResourceName name, CharSequence cs) { - establishPackageRemovedReceiver(); - - synchronized (sSync) { - sStringCache.put(name, new WeakReference(cs)); - } - } - - private CharSequence getLabel(ResourceName name, ApplicationInfo app, int id) { - CharSequence cs = getCachedString(name); - if (cs != null) { - return cs; - } - try { - Resources r = getResourcesForApplication(app); - cs = r.getText(id); - putCachedString(name, cs); - } catch (NameNotFoundException e) { - Log.w("PackageManager", "Failure retrieving resources for" - + app.packageName); - } catch (RuntimeException e) { - // If an exception was thrown, fall through to return null - Log.w("ApplicationInfo", "Failure retrieving activity name", e); - } - return cs; - } - - @Override - public CharSequence getText(String packageName, int resid, - ApplicationInfo appInfo) { - ResourceName name = new ResourceName(packageName, resid); - CharSequence text = getCachedString(name); - if (text != null) { - return text; - } - if (appInfo == null) { - try { - appInfo = getApplicationInfo(packageName, 0); - } catch (NameNotFoundException e) { - return null; - } - } - try { - Resources r = getResourcesForApplication(appInfo); - text = r.getText(resid); - putCachedString(name, text); - return text; - } catch (NameNotFoundException e) { - Log.w("PackageManager", "Failure retrieving resources for" - + appInfo.packageName); - } catch (RuntimeException e) { - // If an exception was thrown, fall through to return - // default icon. - Log.w("PackageManager", "Failure retrieving text 0x" - + Integer.toHexString(resid) + " in package " - + packageName, e); - } - return null; - } - - @Override - public XmlResourceParser getXml(String packageName, int resid, - ApplicationInfo appInfo) { - if (appInfo == null) { - try { - appInfo = getApplicationInfo(packageName, 0); - } catch (NameNotFoundException e) { - return null; - } - } - try { - Resources r = getResourcesForApplication(appInfo); - return r.getXml(resid); - } catch (RuntimeException e) { - // If an exception was thrown, fall through to return - // default icon. - Log.w("PackageManager", "Failure retrieving xml 0x" - + Integer.toHexString(resid) + " in package " - + packageName, e); - } catch (NameNotFoundException e) { - Log.w("PackageManager", "Failure retrieving resources for" - + appInfo.packageName); - } - return null; - } - - @Override - public CharSequence getApplicationLabel(ApplicationInfo info) { - if (info.nonLocalizedLabel != null) { - return info.nonLocalizedLabel; - } - final int id = info.labelRes; - if (id != 0) { - CharSequence cs = getLabel(new ResourceName(info, id), info, id); - if (cs != null) { - return cs; - } - } - return info.packageName; - } - - @Override - public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags, - String installerPackageName) { - try { - mPM.installPackage(packageURI, observer, flags, installerPackageName); - } catch (RemoteException e) { - // Should never happen! - } - } - - @Override - public String getInstallerPackageName(String packageName) { - try { - return mPM.getInstallerPackageName(packageName); - } catch (RemoteException e) { - // Should never happen! - } - return null; - } - - @Override - public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) { - try { - mPM.deletePackage(packageName, observer, flags); - } catch (RemoteException e) { - // Should never happen! - } - } - @Override - public void clearApplicationUserData(String packageName, - IPackageDataObserver observer) { - try { - mPM.clearApplicationUserData(packageName, observer); - } catch (RemoteException e) { - // Should never happen! - } - } - @Override - public void deleteApplicationCacheFiles(String packageName, - IPackageDataObserver observer) { - try { - mPM.deleteApplicationCacheFiles(packageName, observer); - } catch (RemoteException e) { - // Should never happen! - } - } - @Override - public void freeStorageAndNotify(long idealStorageSize, IPackageDataObserver observer) { - try { - mPM.freeStorageAndNotify(idealStorageSize, observer); - } catch (RemoteException e) { - // Should never happen! - } - } - - @Override - public void freeStorage(long freeStorageSize, IntentSender pi) { - try { - mPM.freeStorage(freeStorageSize, pi); - } catch (RemoteException e) { - // Should never happen! - } - } - - @Override - public void getPackageSizeInfo(String packageName, - IPackageStatsObserver observer) { - try { - mPM.getPackageSizeInfo(packageName, observer); - } catch (RemoteException e) { - // Should never happen! - } - } - @Override - public void addPackageToPreferred(String packageName) { - try { - mPM.addPackageToPreferred(packageName); - } catch (RemoteException e) { - // Should never happen! - } - } - - @Override - public void removePackageFromPreferred(String packageName) { - try { - mPM.removePackageFromPreferred(packageName); - } catch (RemoteException e) { - // Should never happen! - } - } - - @Override - public List getPreferredPackages(int flags) { - try { - return mPM.getPreferredPackages(flags); - } catch (RemoteException e) { - // Should never happen! - } - return new ArrayList(); - } - - @Override - public void addPreferredActivity(IntentFilter filter, - int match, ComponentName[] set, ComponentName activity) { - try { - mPM.addPreferredActivity(filter, match, set, activity); - } catch (RemoteException e) { - // Should never happen! - } - } - - @Override - public void replacePreferredActivity(IntentFilter filter, - int match, ComponentName[] set, ComponentName activity) { - try { - mPM.replacePreferredActivity(filter, match, set, activity); - } catch (RemoteException e) { - // Should never happen! - } - } - - @Override - public void clearPackagePreferredActivities(String packageName) { - try { - mPM.clearPackagePreferredActivities(packageName); - } catch (RemoteException e) { - // Should never happen! - } - } - - @Override - public int getPreferredActivities(List outFilters, - List outActivities, String packageName) { - try { - return mPM.getPreferredActivities(outFilters, outActivities, packageName); - } catch (RemoteException e) { - // Should never happen! - } - return 0; - } - - @Override - public void setComponentEnabledSetting(ComponentName componentName, - int newState, int flags) { - try { - mPM.setComponentEnabledSetting(componentName, newState, flags); - } catch (RemoteException e) { - // Should never happen! - } - } - - @Override - public int getComponentEnabledSetting(ComponentName componentName) { - try { - return mPM.getComponentEnabledSetting(componentName); - } catch (RemoteException e) { - // Should never happen! - } - return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; - } - - @Override - public void setApplicationEnabledSetting(String packageName, - int newState, int flags) { - try { - mPM.setApplicationEnabledSetting(packageName, newState, flags); - } catch (RemoteException e) { - // Should never happen! - } - } - - @Override - public int getApplicationEnabledSetting(String packageName) { - try { - return mPM.getApplicationEnabledSetting(packageName); - } catch (RemoteException e) { - // Should never happen! - } - return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; - } - - private final ApplicationContext mContext; - private final IPackageManager mPM; - - private static final Object sSync = new Object(); - private static final Object sPackageRemovedSync = new Object(); - private static BroadcastReceiver sPackageRemovedReceiver; - private static HashMap > sIconCache - = new HashMap >(); - private static HashMap > sStringCache - = new HashMap >(); - } - - // ---------------------------------------------------------------------- - // ---------------------------------------------------------------------- - // ---------------------------------------------------------------------- - - private static final class SharedPreferencesImpl implements SharedPreferences { - - private final File mFile; - private final File mBackupFile; - private final int mMode; - private Map mMap; - private final FileStatus mFileStatus = new FileStatus(); - private long mTimestamp; - - private static final Object mContent = new Object(); - private WeakHashMap mListeners; - - SharedPreferencesImpl( - File file, int mode, Map initialContents) { - mFile = file; - mBackupFile = makeBackupFile(file); - mMode = mode; - mMap = initialContents != null ? initialContents : new HashMap(); - if (FileUtils.getFileStatus(file.getPath(), mFileStatus)) { - mTimestamp = mFileStatus.mtime; - } - mListeners = new WeakHashMap(); - } - - public boolean hasFileChanged() { - synchronized (this) { - if (!FileUtils.getFileStatus(mFile.getPath(), mFileStatus)) { - return true; - } - return mTimestamp != mFileStatus.mtime; - } - } - - public void replace(Map newContents) { - if (newContents != null) { - synchronized (this) { - mMap = newContents; - } - } - } - - public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { - synchronized(this) { - mListeners.put(listener, mContent); - } - } - - public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { - synchronized(this) { - mListeners.remove(listener); - } - } - - public Map getAll() { - synchronized(this) { - //noinspection unchecked - return new HashMap(mMap); - } - } - - public String getString(String key, String defValue) { - synchronized (this) { - String v = (String)mMap.get(key); - return v != null ? v : defValue; - } - } - - public int getInt(String key, int defValue) { - synchronized (this) { - Integer v = (Integer)mMap.get(key); - return v != null ? v : defValue; - } - } - public long getLong(String key, long defValue) { - synchronized (this) { - Long v = (Long) mMap.get(key); - return v != null ? v : defValue; - } - } - public float getFloat(String key, float defValue) { - synchronized (this) { - Float v = (Float)mMap.get(key); - return v != null ? v : defValue; - } - } - public boolean getBoolean(String key, boolean defValue) { - synchronized (this) { - Boolean v = (Boolean)mMap.get(key); - return v != null ? v : defValue; - } - } - - public boolean contains(String key) { - synchronized (this) { - return mMap.containsKey(key); - } - } - - public final class EditorImpl implements Editor { - private final Map mModified = Maps.newHashMap(); - private boolean mClear = false; - - public Editor putString(String key, String value) { - synchronized (this) { - mModified.put(key, value); - return this; - } - } - public Editor putInt(String key, int value) { - synchronized (this) { - mModified.put(key, value); - return this; - } - } - public Editor putLong(String key, long value) { - synchronized (this) { - mModified.put(key, value); - return this; - } - } - public Editor putFloat(String key, float value) { - synchronized (this) { - mModified.put(key, value); - return this; - } - } - public Editor putBoolean(String key, boolean value) { - synchronized (this) { - mModified.put(key, value); - return this; - } - } - - public Editor remove(String key) { - synchronized (this) { - mModified.put(key, this); - return this; - } - } - - public Editor clear() { - synchronized (this) { - mClear = true; - return this; - } - } - - public boolean commit() { - boolean returnValue; - - boolean hasListeners; - List keysModified = null; - Set listeners = null; - - synchronized (SharedPreferencesImpl.this) { - hasListeners = mListeners.size() > 0; - if (hasListeners) { - keysModified = new ArrayList(); - listeners = - new HashSet(mListeners.keySet()); - } - - synchronized (this) { - if (mClear) { - mMap.clear(); - mClear = false; - } - - for (Entry e : mModified.entrySet()) { - String k = e.getKey(); - Object v = e.getValue(); - if (v == this) { - mMap.remove(k); - } else { - mMap.put(k, v); - } - - if (hasListeners) { - keysModified.add(k); - } - } - - mModified.clear(); - } - - returnValue = writeFileLocked(); - } - - if (hasListeners) { - for (int i = keysModified.size() - 1; i >= 0; i--) { - final String key = keysModified.get(i); - for (OnSharedPreferenceChangeListener listener : listeners) { - if (listener != null) { - listener.onSharedPreferenceChanged(SharedPreferencesImpl.this, key); - } - } - } - } - - return returnValue; - } - } - - public Editor edit() { - return new EditorImpl(); - } - - private FileOutputStream createFileOutputStream(File file) { - FileOutputStream str = null; - try { - str = new FileOutputStream(file); - } catch (FileNotFoundException e) { - File parent = file.getParentFile(); - if (!parent.mkdir()) { - Log.e(TAG, "Couldn't create directory for SharedPreferences file " + file); - return null; - } - FileUtils.setPermissions( - parent.getPath(), - FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, - -1, -1); - try { - str = new FileOutputStream(file); - } catch (FileNotFoundException e2) { - Log.e(TAG, "Couldn't create SharedPreferences file " + file, e2); - } - } - return str; - } - - private boolean writeFileLocked() { - // Rename the current file so it may be used as a backup during the next read - if (mFile.exists()) { - if (!mFile.renameTo(mBackupFile)) { - Log.e(TAG, "Couldn't rename file " + mFile + " to backup file " + mBackupFile); - return false; - } - } - - // Attempt to write the file, delete the backup and return true as atomically as - // possible. If any exception occurs, delete the new file; next time we will restore - // from the backup. - try { - FileOutputStream str = createFileOutputStream(mFile); - if (str == null) { - return false; - } - XmlUtils.writeMapXml(mMap, str); - str.close(); - setFilePermissionsFromMode(mFile.getPath(), mMode, 0); - if (FileUtils.getFileStatus(mFile.getPath(), mFileStatus)) { - mTimestamp = mFileStatus.mtime; - } - - // Writing was successful, delete the backup file if there is one. - mBackupFile.delete(); - return true; - } catch (XmlPullParserException e) { - Log.w(TAG, "writeFileLocked: Got exception:", e); - } catch (IOException e) { - Log.w(TAG, "writeFileLocked: Got exception:", e); - } - // Clean up an unsuccessfully written file - if (mFile.exists()) { - if (!mFile.delete()) { - Log.e(TAG, "Couldn't clean up partially-written file " + mFile); - } - } - return false; - } - } -} From 9bf3bb21c097cc96f30d9bf3ec7dea62a74a738b Mon Sep 17 00:00:00 2001 From: Szymon Jakubczak Date: Wed, 9 Jun 2010 16:10:19 -0400 Subject: [PATCH 8/8] - updates NetUtils to use new libnetutils headers - updates WifiStateTracker to track net.XXX.dnsX properties for name servers - removes dhcp from WifiNative (use NetworkUtils instead) Change-Id: Ic69ff253e8784cad34e9291e7970ee38bfa235b9 --- core/jni/android_net_NetUtils.cpp | 25 +-------- core/jni/android_net_wifi_Wifi.cpp | 54 ------------------- wifi/java/android/net/wifi/WifiNative.java | 6 --- .../android/net/wifi/WifiStateTracker.java | 17 ++---- 4 files changed, 5 insertions(+), 97 deletions(-) diff --git a/core/jni/android_net_NetUtils.cpp b/core/jni/android_net_NetUtils.cpp index feb0dadc15b1d..50df9d3fc0474 100644 --- a/core/jni/android_net_NetUtils.cpp +++ b/core/jni/android_net_NetUtils.cpp @@ -22,29 +22,8 @@ #include #include -extern "C" { -int ifc_enable(const char *ifname); -int ifc_disable(const char *ifname); -int ifc_add_host_route(const char *ifname, uint32_t addr); -int ifc_remove_host_routes(const char *ifname); -int ifc_set_default_route(const char *ifname, uint32_t gateway); -int ifc_get_default_route(const char *ifname); -int ifc_remove_default_route(const char *ifname); -int ifc_reset_connections(const char *ifname); -int ifc_configure(const char *ifname, in_addr_t ipaddr, in_addr_t netmask, in_addr_t gateway, in_addr_t dns1, in_addr_t dns2); - -int dhcp_do_request(const char *ifname, - in_addr_t *ipaddr, - in_addr_t *gateway, - in_addr_t *mask, - in_addr_t *dns1, - in_addr_t *dns2, - in_addr_t *server, - uint32_t *lease); -int dhcp_stop(const char *ifname); -int dhcp_release_lease(const char *ifname); -char *dhcp_get_errmsg(); -} +#include +#include #define NETUTILS_PKG_NAME "android/net/NetworkUtils" diff --git a/core/jni/android_net_wifi_Wifi.cpp b/core/jni/android_net_wifi_Wifi.cpp index f5d9eb0bf91f4..4c84bd5a0108b 100644 --- a/core/jni/android_net_wifi_Wifi.cpp +++ b/core/jni/android_net_wifi_Wifi.cpp @@ -29,23 +29,6 @@ namespace android { static jboolean sScanModeActive = false; -/* - * The following remembers the jfieldID's of the fields - * of the DhcpInfo Java object, so that we don't have - * to look them up every time. - */ -static struct fieldIds { - jclass dhcpInfoClass; - jmethodID constructorId; - jfieldID ipaddress; - jfieldID gateway; - jfieldID netmask; - jfieldID dns1; - jfieldID dns2; - jfieldID serverAddress; - jfieldID leaseDuration; -} dhcpInfoFieldIds; - static int doCommand(const char *cmd, char *replybuf, int replybuflen) { size_t reply_len = replybuflen - 1; @@ -491,28 +474,6 @@ static jboolean android_net_wifi_clearBlacklistCommand(JNIEnv* env, jobject claz return doBooleanCommand("BLACKLIST clear", "OK"); } -static jboolean android_net_wifi_doDhcpRequest(JNIEnv* env, jobject clazz, jobject info) -{ - jint ipaddr, gateway, mask, dns1, dns2, server, lease; - jboolean succeeded = ((jboolean)::do_dhcp_request(&ipaddr, &gateway, &mask, - &dns1, &dns2, &server, &lease) == 0); - if (succeeded && dhcpInfoFieldIds.dhcpInfoClass != NULL) { - env->SetIntField(info, dhcpInfoFieldIds.ipaddress, ipaddr); - env->SetIntField(info, dhcpInfoFieldIds.gateway, gateway); - env->SetIntField(info, dhcpInfoFieldIds.netmask, mask); - env->SetIntField(info, dhcpInfoFieldIds.dns1, dns1); - env->SetIntField(info, dhcpInfoFieldIds.dns2, dns2); - env->SetIntField(info, dhcpInfoFieldIds.serverAddress, server); - env->SetIntField(info, dhcpInfoFieldIds.leaseDuration, lease); - } - return succeeded; -} - -static jstring android_net_wifi_getDhcpError(JNIEnv* env, jobject clazz) -{ - return env->NewStringUTF(::get_dhcp_error_string()); -} - // ---------------------------------------------------------------------------- /* @@ -569,9 +530,6 @@ static JNINativeMethod gWifiMethods[] = { { "setScanResultHandlingCommand", "(I)Z", (void*) android_net_wifi_setScanResultHandlingCommand }, { "addToBlacklistCommand", "(Ljava/lang/String;)Z", (void*) android_net_wifi_addToBlacklistCommand }, { "clearBlacklistCommand", "()Z", (void*) android_net_wifi_clearBlacklistCommand }, - - { "doDhcpRequest", "(Landroid/net/DhcpInfo;)Z", (void*) android_net_wifi_doDhcpRequest }, - { "getDhcpError", "()Ljava/lang/String;", (void*) android_net_wifi_getDhcpError }, }; int register_android_net_wifi_WifiManager(JNIEnv* env) @@ -579,18 +537,6 @@ int register_android_net_wifi_WifiManager(JNIEnv* env) jclass wifi = env->FindClass(WIFI_PKG_NAME); LOG_FATAL_IF(wifi == NULL, "Unable to find class " WIFI_PKG_NAME); - dhcpInfoFieldIds.dhcpInfoClass = env->FindClass("android/net/DhcpInfo"); - if (dhcpInfoFieldIds.dhcpInfoClass != NULL) { - dhcpInfoFieldIds.constructorId = env->GetMethodID(dhcpInfoFieldIds.dhcpInfoClass, "", "()V"); - dhcpInfoFieldIds.ipaddress = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "ipAddress", "I"); - dhcpInfoFieldIds.gateway = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "gateway", "I"); - dhcpInfoFieldIds.netmask = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "netmask", "I"); - dhcpInfoFieldIds.dns1 = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "dns1", "I"); - dhcpInfoFieldIds.dns2 = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "dns2", "I"); - dhcpInfoFieldIds.serverAddress = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "serverAddress", "I"); - dhcpInfoFieldIds.leaseDuration = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "leaseDuration", "I"); - } - return AndroidRuntime::registerNativeMethods(env, WIFI_PKG_NAME, gWifiMethods, NELEM(gWifiMethods)); } diff --git a/wifi/java/android/net/wifi/WifiNative.java b/wifi/java/android/net/wifi/WifiNative.java index d9378fcd01746..9b8e5ac83d890 100644 --- a/wifi/java/android/net/wifi/WifiNative.java +++ b/wifi/java/android/net/wifi/WifiNative.java @@ -16,8 +16,6 @@ package android.net.wifi; -import android.net.DhcpInfo; - /** * Native calls for sending requests to the supplicant daemon, and for * receiving asynchronous events. All methods of the form "xxxxCommand()" @@ -142,10 +140,6 @@ public class WifiNative { public native static boolean clearBlacklistCommand(); - public native static boolean doDhcpRequest(DhcpInfo results); - - public native static String getDhcpError(); - /** * Wait for the supplicant to send an event, returning the event string. * @return the event string sent by the supplicant. diff --git a/wifi/java/android/net/wifi/WifiStateTracker.java b/wifi/java/android/net/wifi/WifiStateTracker.java index d0f0e718d8d68..c396a83a9443f 100644 --- a/wifi/java/android/net/wifi/WifiStateTracker.java +++ b/wifi/java/android/net/wifi/WifiStateTracker.java @@ -295,8 +295,6 @@ public class WifiStateTracker extends NetworkStateTracker { private Runnable mReleaseWakeLockCallback; - private static String[] sDnsPropNames; - /** * A structure for supplying information about a supplicant state * change in the STATE_CHANGE event message that comes from the @@ -351,9 +349,9 @@ public class WifiStateTracker extends NetworkStateTracker { mSettingsObserver = new SettingsObserver(new Handler()); mInterfaceName = SystemProperties.get("wifi.interface", "tiwlan0"); - sDnsPropNames = new String[] { - "dhcp." + mInterfaceName + ".dns1", - "dhcp." + mInterfaceName + ".dns2" + mDnsPropNames = new String[] { + "net." + mInterfaceName + ".dns1", + "net." + mInterfaceName + ".dns2" }; mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo")); @@ -397,15 +395,6 @@ public class WifiStateTracker extends NetworkStateTracker { updateNetworkInfo(); } - /** - * Return the IP addresses of the DNS servers available for the WLAN - * network interface. - * @return a list of DNS addresses, with no holes. - */ - public String[] getNameServers() { - return getNameServerList(sDnsPropNames); - } - /** * Return the name of our WLAN network interface. * @return the name of our interface.