private Map<String, Long> getMobileData(Context context, TelephonyManager tm, NetworkStatsManager nsm, int offset) { Map<String, Long> result = new HashMap<>(); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { long[] range = AppUtil.getTimeRange(SortEnum.getSortEnum(offset)); NetworkStats networkStatsM; try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { networkStatsM = nsm.querySummary(ConnectivityManager.TYPE_MOBILE, tm.getSubscriberId(), range[0], range[1]); if (networkStatsM != null) { while (networkStatsM.hasNextBucket()) { NetworkStats.Bucket bucket = new NetworkStats.Bucket(); networkStatsM.getNextBucket(bucket); String key = "u" + bucket.getUid(); if (result.containsKey(key)) { result.put(key, result.get(key) + bucket.getTxBytes() + bucket.getRxBytes()); } else { result.put(key, bucket.getTxBytes() + bucket.getRxBytes()); } } } } } catch (RemoteException e) { e.printStackTrace(); } } return result; }
@TargetApi(23) public static NetworkStatsManager getNetworkStatsManager() { return (NetworkStatsManager) getSystemService(Context.NETWORK_STATS_SERVICE); }
/** * @since 4.0.0 */ public static NetworkStatsManager networkStats(Context context) { return (NetworkStatsManager) context.getSystemService(NETWORK_STATS_SERVICE); }
@Override public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); View view = inflater.inflate(R.layout.network_usage_stats, container, false); getActivity().getActionBar().setTitle(R.string.data_usage); mPackageManager = getActivity().getPackageManager(); mNetstatsManager = (NetworkStatsManager)getActivity().getSystemService( Context.NETWORK_STATS_SERVICE); mListData = new ArrayList<>(); mListAdapter = new ArrayAdapter<List<NetworkStats.Bucket>>(getActivity(), R.layout.data_usage_item, android.R.id.title, mListData) { @Override public View getView(int position, View convertView, ViewGroup parent) { View appView = convertView; if (convertView == null) { appView = inflater.inflate(R.layout.data_usage_item, parent, false); final TextView title = (TextView) appView.findViewById(android.R.id.title); final TextView summary = (TextView) appView.findViewById(android.R.id.summary); final TextView state = (TextView) appView.findViewById(R.id.state); ImageView imageView = (ImageView) appView.findViewById(android.R.id.icon); appView.setTag(new ViewHolder(title, summary, state, imageView)); } List<NetworkStats.Bucket> item = getItem(position); bindView(appView, item); return appView; } }; mQuerySpinner = (Spinner) view.findViewById(R.id.query_type_spinner); mQuerySpinner.setOnItemSelectedListener(this); mExplanation = (TextView) view.findViewById(R.id.explanation); mStartDate = getTodayPlus(0, 0); mEndDate = getTodayPlus(Calendar.DAY_OF_MONTH, 1); mStartDateButton = (Button) view.findViewById(R.id.start_date_button); if (mStartDateButton != null) { mStartDateButton.setOnClickListener(this); } mEndDateButton = (Button) view.findViewById(R.id.end_date_button); if (mEndDateButton != null) { mEndDateButton.setOnClickListener(this); } updateButtonsText(); mDataUsageSummary = (TextView) view.findViewById(R.id.data_usage_summary); mDataUsageList = (ListView) view.findViewById(android.R.id.list); mDataUsageList.setAdapter(mListAdapter); mAppHistoryList = (ListView) view.findViewById(R.id.app_history); mBackToAppsListButton = (Button) view.findViewById(R.id.back_to_apps_button); if (mBackToAppsListButton != null) { mBackToAppsListButton.setOnClickListener(this); } return view; }
@TargetApi(AndroidHelper.API_23) public static NetworkStatsManager networkStatsManager() { return (NetworkStatsManager) get(Context.NETWORK_STATS_SERVICE); }
/** * Obtain a {@link NetworkStatsManager} instance associated with specified {@link Context} * * @param context Context * @return {@link NetworkStatsManager} associated with specified {@link Context} * @throws InvalidContextException if {@link NetworkStatsManager} can't be obtained * from specified {@link Context} */ @NonNull @RequiresApi(Build.VERSION_CODES.M) public static NetworkStatsManager getNetworkStatsManager(@NonNull Context context) { return validate(context.getSystemService(Context.NETWORK_STATS_SERVICE)); }