import android.graphics.Color;
import android.net.INetworkPolicyManager;
import android.net.INetworkStatsService;
+import android.net.NetworkPolicyManager;
import android.net.NetworkStatsHistory;
import android.os.Bundle;
import android.os.RemoteException;
private static final boolean LOGD = true;
private int mUid;
+ private Intent mAppSettingsIntent;
private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
public void onResume() {
super.onResume();
- final Context context = getActivity();
-
- mUid = getArguments().getInt(Intent.EXTRA_UID);
- mTitle.setText(context.getPackageManager().getNameForUid(mUid));
-
updateBody();
}
private void updateBody() {
+ final PackageManager pm = getActivity().getPackageManager();
+
+ mUid = getArguments().getInt(Intent.EXTRA_UID);
+ mTitle.setText(pm.getNameForUid(mUid));
+
+ // enable settings button when package provides it
+ // TODO: target torwards entire UID instead of just first package
+ final String[] packageNames = pm.getPackagesForUid(mUid);
+ if (packageNames != null && packageNames.length > 0) {
+ mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
+ mAppSettingsIntent.setPackage(packageNames[0]);
+ mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
+
+ final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
+ mAppSettings.setEnabled(matchFound);
+
+ } else {
+ mAppSettingsIntent = null;
+ mAppSettings.setEnabled(false);
+ }
+
try {
// load stats for current uid and template
// TODO: read template from extras
- mUidPolicy = mPolicyService.getUidPolicy(mUid);
mHistory = mStatsService.getHistoryForUid(mUid, TEMPLATE_MOBILE_ALL);
} catch (RemoteException e) {
- // since we can't do much without policy or history, and we don't
- // want to leave with half-baked UI, we bail hard.
+ // since we can't do much without history, and we don't want to
+ // leave with half-baked UI, we bail hard.
throw new RuntimeException("problem reading network stats", e);
}
mChart.setVisibleRange(bounds[0], bounds[1] + DateUtils.WEEK_IN_MILLIS, bounds[1]);
updateDetailData();
- // update policy checkbox
- final boolean restrictBackground = (mUidPolicy & POLICY_REJECT_PAID_BACKGROUND) != 0;
- mRestrictBackground.setChecked(restrictBackground);
+ final Context context = getActivity();
+ if (NetworkPolicyManager.isUidValidForPolicy(context, mUid)) {
+ mRestrictBackgroundView.setVisibility(View.VISIBLE);
+
+ final int uidPolicy;
+ try {
+ uidPolicy = mPolicyService.getUidPolicy(mUid);
+ } catch (RemoteException e) {
+ // since we can't do much without policy, we bail hard.
+ throw new RuntimeException("problem reading network policy", e);
+ }
- // kick preference views so they rebind from changes above
- refreshPreferenceViews();
+ // update policy checkbox
+ final boolean restrictBackground = (mUidPolicy & POLICY_REJECT_PAID_BACKGROUND) != 0;
+ mRestrictBackground.setChecked(restrictBackground);
+
+ // kick preference views so they rebind from changes above
+ refreshPreferenceViews();
+
+ } else {
+ mRestrictBackgroundView.setVisibility(View.GONE);
+ }
}
private void updateDetailData() {
/** {@inheritDoc} */
public void onClick(View v) {
// TODO: target torwards entire UID instead of just first package
- final PackageManager pm = getActivity().getPackageManager();
- final String packageName = pm.getPackagesForUid(mUid)[0];
-
- final Intent intent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
- intent.setPackage(packageName);
- startActivity(intent);
+ startActivity(mAppSettingsIntent);
}
};
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
import android.net.INetworkPolicyManager;
import android.net.INetworkStatsService;
import android.net.NetworkPolicy;
import android.preference.PreferenceActivity;
import android.preference.SwitchPreference;
import android.telephony.TelephonyManager;
+import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.format.Formatter;
import android.text.format.Time;
mCycleSpinner.setSelection(0);
} else {
- if (LOGD) Log.d(TAG, "showing cycle " + cycle);
+ if (LOGD) {
+ Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
+ + cycle.end + "]");
+ }
// update chart to show selected cycle, and update detail data
// to match updated sweep bounds.
mItems.clear();
for (int i = 0; i < stats.size; i++) {
- final AppUsageItem item = new AppUsageItem();
- item.uid = stats.uid[i];
- item.total = stats.rx[i] + stats.tx[i];
- mItems.add(item);
+ final long total = stats.rx[i] + stats.tx[i];
+ if (total > 0) {
+ final AppUsageItem item = new AppUsageItem();
+ item.uid = stats.uid[i];
+ item.total = total;
+ mItems.add(item);
+ }
}
Collections.sort(mItems);
final TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);
final AppUsageItem item = mItems.get(position);
- text1.setText(pm.getNameForUid(item.uid));
+ text1.setText(resolveLabelForUid(pm, item.uid));
text2.setText(Formatter.formatFileSize(context, item.total));
return convertView;
}
}
+ /**
+ * Resolve best descriptive label for the given UID.
+ */
+ public static CharSequence resolveLabelForUid(PackageManager pm, int uid) {
+ final String[] packageNames = pm.getPackagesForUid(uid);
+ final int length = packageNames != null ? packageNames.length : 0;
+
+ CharSequence label = pm.getNameForUid(uid);
+ try {
+ if (length == 1) {
+ final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0);
+ label = info.loadLabel(pm);
+ } else if (length > 1) {
+ for (String packageName : packageNames) {
+ final PackageInfo info = pm.getPackageInfo(packageName, 0);
+ if (info.sharedUserLabel != 0) {
+ label = pm.getText(packageName, info.sharedUserLabel, info.applicationInfo);
+ if (!TextUtils.isEmpty(label)) {
+ break;
+ }
+ }
+ }
+ }
+ } catch (NameNotFoundException e) {
+ }
+
+ if (TextUtils.isEmpty(label)) {
+ label = Integer.toString(uid);
+ }
+ return label;
+ }
+
}