Skip to content

Commit 1adbdc2

Browse files
committed
Fix date string in app usage history
See #1 and #2 for requirements. Bug: 24492965 Change-Id: I98388caac40c9898a28a32d3154500f716aacfa3
1 parent 657f263 commit 1adbdc2

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

app/src/main/java/com/afwsamples/testdpc/policy/networking/NetworkUsageStatsFragment.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import android.app.AlertDialog;
2121
import android.app.DatePickerDialog;
2222
import android.app.ListFragment;
23-
import android.app.usage.NetworkStatsManager;
2423
import android.app.usage.NetworkStats;
24+
import android.app.usage.NetworkStatsManager;
2525
import android.content.Context;
2626
import android.content.pm.ApplicationInfo;
2727
import android.content.pm.PackageInfo;
@@ -32,6 +32,8 @@
3232
import android.os.Build;
3333
import android.os.Bundle;
3434
import android.os.RemoteException;
35+
import android.text.Html;
36+
import android.text.Spanned;
3537
import android.text.format.Formatter;
3638
import android.util.SparseArray;
3739
import android.view.LayoutInflater;
@@ -49,6 +51,7 @@
4951
import com.afwsamples.testdpc.R;
5052

5153
import java.text.DateFormat;
54+
import java.text.SimpleDateFormat;
5255
import java.util.ArrayList;
5356
import java.util.Arrays;
5457
import java.util.Calendar;
@@ -85,6 +88,15 @@ public class NetworkUsageStatsFragment extends ListFragment implements View.OnCl
8588
private ArrayAdapter<List<NetworkStats.Bucket>> mListAdapter;
8689
private ListView mAppHistoryList;
8790
private Button mBackToAppsListButton;
91+
private DateFormat mDateStringFormat;
92+
private DateFormat mHourMinuteDateFormat;
93+
94+
@Override
95+
public void onCreate(Bundle savedInstanceState) {
96+
super.onCreate(savedInstanceState);
97+
mDateStringFormat = new SimpleDateFormat("*dd/MM/YYYY*");
98+
mHourMinuteDateFormat = new SimpleDateFormat("kk:mm");
99+
}
88100

89101
@Override
90102
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
@@ -256,22 +268,29 @@ public View getView(int position, View convertView, ViewGroup parent) {
256268
View view = convertView;
257269
if (convertView == null) {
258270
view = getActivity().getLayoutInflater().inflate(
259-
android.R.layout.two_line_list_item, parent, false);
271+
R.layout.network_usage_app_history_item, parent, false);
260272
}
261-
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
262273
NetworkStats.Bucket item = getItem(position);
263274
Date startDate = new Date(item.getStartTimeStamp());
264275
Date endDate = new Date(item.getEndTimeStamp());
265-
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
266-
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
267-
text1.setText(dateFormat.format(startDate) + " - "
268-
+ dateFormat.format(endDate));
276+
TextView text1 = (TextView) view.findViewById(R.id.text1);
277+
TextView text2 = (TextView) view.findViewById(R.id.text2);
278+
text1.setText(getDateString(startDate, endDate));
269279
text2.setText(getString(R.string.network_stats_bucket_usage,
270280
formatSize(item.getRxBytes()), item.getRxPackets(),
271281
formatSize(item.getTxBytes()), item.getTxPackets()));
272282
return view;
273283
}
274-
};
284+
285+
private Spanned getDateString(Date startDate, Date endDate) {
286+
String startDateString = mDateStringFormat.format(startDate);
287+
String startHourMinuteString = mHourMinuteDateFormat.format(startDate);
288+
String endHourMinuteString = mHourMinuteDateFormat.format(endDate);
289+
String resultString = "<b>" + startDateString + "</b> " +
290+
startHourMinuteString + " - " + endHourMinuteString;
291+
return Html.fromHtml(resultString);
292+
}
293+
};
275294
mAppHistoryList.setAdapter(adapter);
276295
}
277296
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2016 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:orientation="vertical">
22+
23+
<TextView
24+
android:id="@+id/text1"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:textSize="16sp"/>
28+
29+
<TextView
30+
android:id="@+id/text2"
31+
android:layout_width="match_parent"
32+
android:layout_height="wrap_content"
33+
android:textSize="16sp"/>
34+
</LinearLayout>

0 commit comments

Comments
 (0)