Commit 9e4fb27d authored by Sophie Chang's avatar Sophie Chang Committed by Commit Bot

Show initial data savings message and fake chart if user has used < 100KB of data

Bug:867129

Change-Id: Iae1c3a3f0cd4ffa3a153eb826eeb926c9572ca10
Reviewed-on: https://chromium-review.googlesource.com/1157165Reviewed-by: default avatarTheresa <twellington@chromium.org>
Reviewed-by: default avatarDoug Arnett <dougarnett@chromium.org>
Commit-Queue: Sophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#582776}
parent 7b9251fa
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2018 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:targetApi="21"
android:width="64dp"
android:height="64dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/disabled_text_color"
android:fillType="evenOdd"
android:pathData="M8.907 1.945c2.541 0 4.762 1.372 5.978 3.41l1.604-1.018C14.937 1.769 12.127 0.047 8.907 0.047 a8.86 8.86 0 0 0-8.86 8.86c0 0.067 0.008 0.13 0.01 0.196l2.01-1.463c0.598-3.235 3.435-5.695 6.84-5.695" />
<path
android:fillColor="@color/disabled_text_color"
android:fillType="evenOdd"
android:pathData="M17.31 6.104l-7.208 4.369-4.92-2.817-4.818 3.572c1.02 3.766 4.454 6.54 8.543 6.54a8.861 8.861 0 0 0 8.862-8.86c0-0.981-0.166-1.922-0.46-2.804zM8.907 15.87c-2.779 0-5.173-1.64-6.288-4l2.8-2.038 4.928 2.82 5.506-3.415c-0.173 3.686-3.217 6.633-6.946 6.633z" />
<path
android:fillType="evenOdd"
android:pathData="M-3-3h24v24H-3z" />
</vector>
......@@ -11,7 +11,19 @@
android:clipToPadding="false"
android:orientation="vertical" >
<TextView
android:id="@+id/initial_data_savings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:drawablePadding="3dp"
android:drawableTop="@drawable/data_reduction_big"
android:text="@string/data_reduction_initial_title"
android:textAppearance="@style/BlackDisabledText1" />
<LinearLayout
android:id="@+id/data_savings_summary_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
......@@ -68,6 +80,7 @@
<include layout="@layout/data_usage_chart" />
<FrameLayout
android:id="@+id/chart_dates"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
......
......@@ -23,6 +23,7 @@ import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.chromium.base.Callback;
......@@ -31,6 +32,7 @@ import org.chromium.base.Log;
import org.chromium.base.VisibleForTesting;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.net.spdyproxy.DataReductionProxySettings;
import org.chromium.chrome.browser.util.ConversionUtils;
import org.chromium.chrome.browser.util.FileSizeUtil;
import org.chromium.third_party.android.datausagechart.ChartDataUsageView;
import org.chromium.third_party.android.datausagechart.NetworkStats;
......@@ -51,11 +53,19 @@ public class DataReductionStatsPreference extends Preference {
*/
private static final String PREF_DATA_REDUCTION_SITE_BREAKDOWN_ALLOWED_DATE =
"data_reduction_site_breakdown_allowed_date";
/**
* The threshold at which to start showing real data usage and savings, in
* kilobytes.
*/
private static final long SHOW_REAL_DATA_USED_KB_THRESHOLD = 100;
private NetworkStatsHistory mOriginalNetworkStatsHistory;
private NetworkStatsHistory mReceivedNetworkStatsHistory;
private List<DataReductionDataUseItem> mSiteBreakdownItems;
private TextView mInitialDataSavingsTextView;
private LinearLayout mDataSavingsSummaryContainer;
private FrameLayout mDataSavingsChartDatesContainer;
private TextView mDataSavingsTextView;
private TextView mDataUsageTextView;
private TextView mStartDateTextView;
......@@ -63,6 +73,7 @@ public class DataReductionStatsPreference extends Preference {
private Button mResetStatisticsButton;
private ChartDataUsageView mChartDataUsageView;
private DataReductionSiteBreakdownView mDataReductionBreakdownView;
private boolean mShouldShowRealData;
private boolean mIsFirstDayChart;
/** Number of days that the chart will present. */
private int mNumDaysInChart;
......@@ -155,6 +166,10 @@ public class DataReductionStatsPreference extends Preference {
mOriginalNetworkStatsHistory = getNetworkStatsHistory(original, mNumDaysInChart);
mReceivedNetworkStatsHistory = getNetworkStatsHistory(received, mNumDaysInChart);
mShouldShowRealData =
ConversionUtils.bytesToKilobytes(mReceivedNetworkStatsHistory.getTotalBytes())
>= SHOW_REAL_DATA_USED_KB_THRESHOLD;
// Determine the visible start and end points based on the available data and when it was
// last updated.
mVisibleStartTimeMillis = mOriginalNetworkStatsHistory.getStart()
......@@ -163,7 +178,7 @@ public class DataReductionStatsPreference extends Preference {
mVisibleEndTimeMillis = mOriginalNetworkStatsHistory.getEnd()
+ numDaysSinceStatsUpdated.intValue() * DateUtils.DAY_IN_MILLIS;
if (mDataReductionBreakdownView != null
if (mShouldShowRealData && mDataReductionBreakdownView != null
&& currentTimeMillis > ContextUtils.getAppSharedPreferences().getLong(
PREF_DATA_REDUCTION_SITE_BREAKDOWN_ALLOWED_DATE,
Long.MAX_VALUE)) {
......@@ -172,6 +187,7 @@ public class DataReductionStatsPreference extends Preference {
@Override
public void onResult(List<DataReductionDataUseItem> result) {
mSiteBreakdownItems = result;
mDataReductionBreakdownView.setAndDisplayDataUseItems(
mSiteBreakdownItems);
}
......@@ -188,6 +204,11 @@ public class DataReductionStatsPreference extends Preference {
return mNumDaysInChart;
}
@VisibleForTesting
boolean shouldShowRealData() {
return mShouldShowRealData;
}
private static NetworkStatsHistory getNetworkStatsHistory(long[] history, int days) {
if (days > history.length) days = history.length;
NetworkStatsHistory networkStatsHistory = new NetworkStatsHistory(
......@@ -205,17 +226,35 @@ public class DataReductionStatsPreference extends Preference {
return networkStatsHistory;
}
private void setDetailText() {
private void updateDetailView() {
final Context context = getContext();
updateDetailData();
mStartDateTextView.setText(mStartDatePhrase);
mStartDateTextView.setContentDescription(context.getString(
R.string.data_reduction_start_date_content_description, mStartDatePhrase));
mEndDateTextView.setText(mEndDatePhrase);
mEndDateTextView.setContentDescription(context.getString(
R.string.data_reduction_end_date_content_description, mEndDatePhrase));
if (mDataUsageTextView != null) mDataUsageTextView.setText(mReceivedTotalPhrase);
if (mDataSavingsTextView != null) mDataSavingsTextView.setText(mSavingsTotalPhrase);
// updateDetailData also updates some UMA based on the actual data shown, so only update it
// if we are actually showing the chart.
if (mShouldShowRealData) updateDetailData();
mInitialDataSavingsTextView.setVisibility(mShouldShowRealData ? View.GONE : View.VISIBLE);
mDataSavingsSummaryContainer.setVisibility(mShouldShowRealData ? View.VISIBLE : View.GONE);
mChartDataUsageView.setVisibility(mShouldShowRealData ? View.VISIBLE : View.GONE);
mResetStatisticsButton.setVisibility(mShouldShowRealData ? View.VISIBLE : View.GONE);
mDataSavingsChartDatesContainer.setVisibility(
mShouldShowRealData ? View.VISIBLE : View.GONE);
mStartDateTextView.setText(mShouldShowRealData ? mStartDatePhrase : "");
mStartDateTextView.setContentDescription(mShouldShowRealData
? context.getString(R.string.data_reduction_start_date_content_description,
mStartDatePhrase)
: "");
mEndDateTextView.setText(mShouldShowRealData ? mEndDatePhrase : "");
mEndDateTextView.setContentDescription(mShouldShowRealData
? context.getString(R.string.data_reduction_end_date_content_description,
mEndDatePhrase)
: "");
if (mDataUsageTextView != null)
mDataUsageTextView.setText(mShouldShowRealData ? mReceivedTotalPhrase : "");
if (mDataSavingsTextView != null)
mDataSavingsTextView.setText(mShouldShowRealData ? mSavingsTotalPhrase : "");
}
/**
......@@ -234,8 +273,12 @@ public class DataReductionStatsPreference extends Preference {
@Override
protected void onBindView(View view) {
super.onBindView(view);
mInitialDataSavingsTextView = (TextView) view.findViewById(R.id.initial_data_savings);
mDataSavingsSummaryContainer =
(LinearLayout) view.findViewById(R.id.data_savings_summary_container);
mDataUsageTextView = (TextView) view.findViewById(R.id.data_reduction_usage);
mDataSavingsTextView = (TextView) view.findViewById(R.id.data_reduction_savings);
mDataSavingsChartDatesContainer = (FrameLayout) view.findViewById(R.id.chart_dates);
mStartDateTextView = (TextView) view.findViewById(R.id.data_reduction_start_date);
mEndDateTextView = (TextView) view.findViewById(R.id.data_reduction_end_date);
mDataReductionBreakdownView =
......@@ -245,10 +288,9 @@ public class DataReductionStatsPreference extends Preference {
// This will query data usage. Only set mSiteBreakdownItems if the statistics are not
// being queried.
updateReductionStatistics(System.currentTimeMillis());
} else if (mSiteBreakdownItems != null) {
} else if (mSiteBreakdownItems != null && mShouldShowRealData) {
mDataReductionBreakdownView.setAndDisplayDataUseItems(mSiteBreakdownItems);
}
setDetailText();
mChartDataUsageView = (ChartDataUsageView) view.findViewById(R.id.chart);
mChartDataUsageView.bindNetworkStats(
......@@ -264,6 +306,8 @@ public class DataReductionStatsPreference extends Preference {
if (mResetStatisticsButton != null) {
setUpResetStatisticsButton();
}
updateDetailView();
}
private void setUpResetStatisticsButton() {
......@@ -292,7 +336,7 @@ public class DataReductionStatsPreference extends Preference {
DataReductionProxySavingsClearedReason
.USER_ACTION_SETTINGS_MENU);
updateReductionStatistics(now);
setDetailText();
updateDetailView();
notifyChanged();
DataReductionProxyUma.dataReductionProxyUIAction(
DataReductionProxyUma.ACTION_STATS_RESET);
......
......@@ -1190,6 +1190,9 @@ To obtain new licenses, connect to the internet and play your downloaded content
<message name="IDS_DATA_REDUCTION_TITLE" desc="Menu item for Data Saver, which allows users to save mobile data by compressing network traffic.">
Data Saver
</message>
<message name="IDS_DATA_REDUCTION_INITIAL_TITLE" desc="This title states that the below chart will contain the user's data savings after they have started browsing.">
Your data savings will appear here
</message>
<message name="IDS_DATA_REDUCTION_SAVED_LABEL" desc="Summary text for the menu item that states the amount of mobile data that was saved by Data Saver (i.e. XX MB saved). Data Saver allows users to to reduce their mobile data usage by compressing network traffic.">
<ph name="data">%1$s<ex>1.0 GB</ex></ph> saved
</message>
......
08d2cfe33c14562fc5989f5fc45e81de3a23ef92
\ No newline at end of file
......@@ -51,6 +51,7 @@ public class DataReductionStatsPreferenceTest {
private static class TestDataReductionProxySettings extends DataReductionProxySettings {
private long mLastUpdateInMillis;
private long[] mReceivedNetworkStatsHistory;
/**
* Returns the time that the data reduction statistics were last updated.
......@@ -68,6 +69,16 @@ public class DataReductionStatsPreferenceTest {
public void setDataReductionLastUpdateTime(long lastUpdateInMillis) {
mLastUpdateInMillis = lastUpdateInMillis;
}
@Override
public long[] getReceivedNetworkStatsHistory() {
if (mReceivedNetworkStatsHistory == null) return new long[0];
return mReceivedNetworkStatsHistory;
}
public void setReceivedNetworkStatsHistory(long[] receivedNetworkStatsHistory) {
mReceivedNetworkStatsHistory = receivedNetworkStatsHistory;
}
}
@Before
......@@ -250,4 +261,33 @@ public class DataReductionStatsPreferenceTest {
Assert.assertEquals(numDaysDataSaverEnabled + 1, pref.getNumDaysInChart());
}
@Test
@SmallTest
@UiThreadTest
@Feature({"DataReduction"})
public void testShouldShowRealDataWhenEnoughDataIsUsed() throws Throwable {
DataReductionStatsPreference pref = new DataReductionStatsPreference(mContext, null);
long now = System.currentTimeMillis();
long lastUpdateTime = now - DateUtils.DAY_IN_MILLIS;
long dataSaverEnableTime = now - DateUtils.HOUR_IN_MILLIS;
mSettings.setDataReductionLastUpdateTime(lastUpdateTime);
ContextUtils.getAppSharedPreferences()
.edit()
.putLong(DataReductionProxySettings.DATA_REDUCTION_FIRST_ENABLED_TIME,
dataSaverEnableTime)
.apply();
// User has only used 50KB so far.
mSettings.setReceivedNetworkStatsHistory(new long[] {50 * 1024});
pref.updateReductionStatistics(now);
Assert.assertFalse(pref.shouldShowRealData());
// User has now used 100KB.
mSettings.setReceivedNetworkStatsHistory(new long[] {100 * 1024});
pref.updateReductionStatistics(now);
Assert.assertTrue(pref.shouldShowRealData());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment