Index: chrome/android/java/src/org/chromium/chrome/browser/preferences/datareduction/DataReductionStatsPreference.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/datareduction/DataReductionStatsPreference.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/datareduction/DataReductionStatsPreference.java |
index ee08ac4813df4832a65c0343c52b6573b411f2c8..4c3f1775c42675745b1e7cf2a21d66435abfdc22 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/datareduction/DataReductionStatsPreference.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/datareduction/DataReductionStatsPreference.java |
@@ -16,13 +16,19 @@ import android.text.format.DateUtils; |
import android.text.format.Formatter; |
import android.util.AttributeSet; |
import android.view.View; |
+import android.view.View.OnClickListener; |
+import android.widget.Button; |
import android.widget.TextView; |
+import org.chromium.base.Callback; |
import org.chromium.chrome.R; |
+import org.chromium.chrome.browser.ChromeFeatureList; |
import org.chromium.chrome.browser.net.spdyproxy.DataReductionProxySettings; |
import org.chromium.third_party.android.datausagechart.ChartDataUsageView; |
+import org.chromium.third_party.android.datausagechart.NetworkStats; |
import org.chromium.third_party.android.datausagechart.NetworkStatsHistory; |
+import java.util.List; |
import java.util.TimeZone; |
/** |
@@ -34,59 +40,90 @@ public class DataReductionStatsPreference extends Preference { |
private TextView mOriginalSizeTextView; |
private TextView mReceivedSizeTextView; |
+ private TextView mDataSavingsTextView; |
+ private TextView mDataUsageTextView; |
private TextView mPercentReductionTextView; |
private TextView mStartDateTextView; |
private TextView mEndDateTextView; |
+ private Button mResetStatisticsButton; |
private ChartDataUsageView mChartDataUsageView; |
+ private DataReductionSiteBreakdownView mDataReductionBreakdownView; |
private long mLeftPosition; |
private long mRightPosition; |
private Long mCurrentTime; |
private String mOriginalTotalPhrase; |
+ private String mSavingsTotalPhrase; |
private String mReceivedTotalPhrase; |
private String mPercentReductionPhrase; |
private String mStartDatePhrase; |
private String mEndDatePhrase; |
- public DataReductionStatsPreference( |
- Context context, AttributeSet attrs, int defStyle) { |
- super(context, attrs, defStyle); |
- setWidgetLayoutResource(R.layout.data_reduction_stats_layout); |
- } |
- |
public DataReductionStatsPreference(Context context, AttributeSet attrs) { |
super(context, attrs); |
- setWidgetLayoutResource(R.layout.data_reduction_stats_layout); |
+ |
+ if (ChromeFeatureList.isEnabled(ChromeFeatureList.DATA_REDUCTION_SITE_BREAKDOWN)) { |
+ setWidgetLayoutResource(R.layout.data_reduction_stats_layout); |
+ } else { |
+ setWidgetLayoutResource(R.layout.data_reduction_old_stats_layout); |
+ } |
} |
- public DataReductionStatsPreference(Context context) { |
- super(context); |
- setWidgetLayoutResource(R.layout.data_reduction_stats_layout); |
+ @Override |
+ public boolean isEnabled() { |
+ return super.isEnabled(); |
} |
/** |
- * Sets the current statistics for viewing. These include the original total daily size of |
- * received resources before compression, and the actual total daily size of received |
- * resources after compression. The last update time is specified in milliseconds since the |
- * epoch. |
- * @param lastUpdateTimeMillis The last time the statistics were updated. |
- * @param networkStatsHistoryOriginal The history of original content lengths. |
- * @param networkStatsHistoryReceived The history of received content lengths. |
+ * Updates the preference screen to convey current statistics on data reduction. |
*/ |
- public void setReductionStats( |
- long lastUpdateTimeMillis, |
- NetworkStatsHistory networkStatsHistoryOriginal, |
- NetworkStatsHistory networkStatsHistoryReceived) { |
- mCurrentTime = lastUpdateTimeMillis; |
+ public void updateReductionStatistics() { |
+ long original[] = DataReductionProxySettings.getInstance().getOriginalNetworkStatsHistory(); |
+ long received[] = DataReductionProxySettings.getInstance().getReceivedNetworkStatsHistory(); |
+ |
+ mCurrentTime = DataReductionProxySettings.getInstance().getDataReductionLastUpdateTime(); |
mRightPosition = mCurrentTime + DateUtils.HOUR_IN_MILLIS |
- TimeZone.getDefault().getOffset(mCurrentTime); |
- mLeftPosition = lastUpdateTimeMillis - DateUtils.DAY_IN_MILLIS * DAYS_IN_CHART; |
- mOriginalNetworkStatsHistory = networkStatsHistoryOriginal; |
- mReceivedNetworkStatsHistory = networkStatsHistoryReceived; |
+ mLeftPosition = mCurrentTime - DateUtils.DAY_IN_MILLIS * DAYS_IN_CHART; |
+ mOriginalNetworkStatsHistory = getNetworkStatsHistory(original, DAYS_IN_CHART); |
+ mReceivedNetworkStatsHistory = getNetworkStatsHistory(received, DAYS_IN_CHART); |
+ |
+ if (mDataReductionBreakdownView != null) { |
+ DataReductionProxySettings.getInstance().queryDataUsage( |
+ DAYS_IN_CHART, new Callback<List<DataReductionDataUseItem>>() { |
+ @Override |
+ public void onResult(List<DataReductionDataUseItem> result) { |
+ mDataReductionBreakdownView.onQueryDataUsageComplete(result); |
+ } |
+ }); |
+ } |
} |
- @Override |
- public boolean isEnabled() { |
- return super.isEnabled(); |
+ private static NetworkStatsHistory getNetworkStatsHistory(long[] history, int days) { |
+ if (days > history.length) days = history.length; |
+ NetworkStatsHistory networkStatsHistory = new NetworkStatsHistory( |
+ DateUtils.DAY_IN_MILLIS, days, NetworkStatsHistory.FIELD_RX_BYTES); |
+ |
+ DataReductionProxySettings config = DataReductionProxySettings.getInstance(); |
+ long time = config.getDataReductionLastUpdateTime() - days * DateUtils.DAY_IN_MILLIS; |
+ for (int i = history.length - days, bucket = 0; i < history.length; i++, bucket++) { |
+ NetworkStats.Entry entry = new NetworkStats.Entry(); |
+ entry.rxBytes = history[i]; |
+ long startTime = time + (DateUtils.DAY_IN_MILLIS * bucket); |
+ // Spread each day's record over the first hour of the day. |
+ networkStatsHistory.recordData(startTime, startTime + DateUtils.HOUR_IN_MILLIS, entry); |
+ } |
+ return networkStatsHistory; |
+ } |
+ |
+ private void setDetailText() { |
+ updateDetailData(); |
+ mPercentReductionTextView.setText(mPercentReductionPhrase); |
+ mStartDateTextView.setText(mStartDatePhrase); |
+ mEndDateTextView.setText(mEndDatePhrase); |
+ if (mDataUsageTextView != null) mDataUsageTextView.setText(mReceivedTotalPhrase); |
+ if (mDataSavingsTextView != null) mDataSavingsTextView.setText(mSavingsTotalPhrase); |
+ if (mOriginalSizeTextView != null) mOriginalSizeTextView.setText(mOriginalTotalPhrase); |
+ if (mReceivedSizeTextView != null) mReceivedSizeTextView.setText(mReceivedTotalPhrase); |
} |
/** |
@@ -96,17 +133,17 @@ public class DataReductionStatsPreference extends Preference { |
@Override |
protected void onBindView(View view) { |
super.onBindView(view); |
- if (mOriginalTotalPhrase == null) updateDetailData(); |
+ mDataUsageTextView = (TextView) view.findViewById(R.id.data_reduction_usage); |
+ mDataSavingsTextView = (TextView) view.findViewById(R.id.data_reduction_savings); |
mOriginalSizeTextView = (TextView) view.findViewById(R.id.data_reduction_original_size); |
- mOriginalSizeTextView.setText(mOriginalTotalPhrase); |
mReceivedSizeTextView = (TextView) view.findViewById(R.id.data_reduction_compressed_size); |
- mReceivedSizeTextView.setText(mReceivedTotalPhrase); |
mPercentReductionTextView = (TextView) view.findViewById(R.id.data_reduction_percent); |
- mPercentReductionTextView.setText(mPercentReductionPhrase); |
mStartDateTextView = (TextView) view.findViewById(R.id.data_reduction_start_date); |
- mStartDateTextView.setText(mStartDatePhrase); |
mEndDateTextView = (TextView) view.findViewById(R.id.data_reduction_end_date); |
- mEndDateTextView.setText(mEndDatePhrase); |
+ mDataReductionBreakdownView = |
+ (DataReductionSiteBreakdownView) view.findViewById(R.id.breakdown); |
+ updateReductionStatistics(); |
+ setDetailText(); |
mChartDataUsageView = (ChartDataUsageView) view.findViewById(R.id.chart); |
mChartDataUsageView.bindOriginalNetworkStats(mOriginalNetworkStatsHistory); |
@@ -122,6 +159,19 @@ public class DataReductionStatsPreference extends Preference { |
} else { |
dataReductionProxyUnreachableWarning.setVisibility(View.GONE); |
} |
+ |
+ mResetStatisticsButton = (Button) view.findViewById(R.id.data_reduction_reset_statistics); |
+ if (mResetStatisticsButton != null) { |
+ mResetStatisticsButton.setOnClickListener(new OnClickListener() { |
+ @Override |
+ public void onClick(View view) { |
+ DataReductionProxySettings.getInstance().clearDataSavingStatistics(); |
+ updateReductionStatistics(); |
+ setDetailText(); |
+ notifyChanged(); |
+ } |
+ }); |
+ } |
} |
/** |
@@ -140,18 +190,20 @@ public class DataReductionStatsPreference extends Preference { |
final long now = mCurrentTime; |
final Context context = getContext(); |
- NetworkStatsHistory.Entry originalEntry = |
- mOriginalNetworkStatsHistory.getValues(start, end, now, null); |
- // Only received bytes are tracked. |
- final long originalTotalBytes = originalEntry.rxBytes; |
- mOriginalTotalPhrase = Formatter.formatFileSize(context, originalTotalBytes); |
- |
NetworkStatsHistory.Entry compressedEntry = |
mReceivedNetworkStatsHistory.getValues(start, end, now, null); |
// Only received bytes are tracked. |
final long compressedTotalBytes = compressedEntry.rxBytes; |
mReceivedTotalPhrase = Formatter.formatFileSize(context, compressedTotalBytes); |
+ NetworkStatsHistory.Entry originalEntry = |
+ mOriginalNetworkStatsHistory.getValues(start, end, now, null); |
+ // Only received bytes are tracked. |
+ final long originalTotalBytes = originalEntry.rxBytes; |
+ mOriginalTotalPhrase = Formatter.formatFileSize(context, originalTotalBytes); |
+ mSavingsTotalPhrase = |
+ Formatter.formatFileSize(context, originalTotalBytes - compressedTotalBytes); |
+ |
float percentage = 0.0f; |
if (originalTotalBytes > 0L && originalTotalBytes > compressedTotalBytes) { |
percentage = (originalTotalBytes - compressedTotalBytes) / (float) originalTotalBytes; |