aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/data/WatchedHistoryManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/android/tv/data/WatchedHistoryManager.java')
-rw-r--r--src/com/android/tv/data/WatchedHistoryManager.java110
1 files changed, 63 insertions, 47 deletions
diff --git a/src/com/android/tv/data/WatchedHistoryManager.java b/src/com/android/tv/data/WatchedHistoryManager.java
index 3edd7b1a..7187efd1 100644
--- a/src/com/android/tv/data/WatchedHistoryManager.java
+++ b/src/com/android/tv/data/WatchedHistoryManager.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.android.tv.data;
import android.content.Context;
@@ -12,9 +27,8 @@ import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.WorkerThread;
import android.util.Log;
-
-import com.android.tv.common.SharedPreferencesUtils;
-
+import com.android.tv.common.util.SharedPreferencesUtils;
+import com.android.tv.data.api.Channel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -25,13 +39,14 @@ import java.util.concurrent.TimeUnit;
/**
* A class to manage watched history.
*
- * <p>When there is no access to watched table of TvProvider,
- * this class is used to build up watched history and to compute recent channels.
+ * <p>When there is no access to watched table of TvProvider, this class is used to build up watched
+ * history and to compute recent channels.
+ *
* <p>Note that this class is not thread safe. Please use this on one thread.
*/
public class WatchedHistoryManager {
- private final static String TAG = "WatchedHistoryManager";
- private final static boolean DEBUG = false;
+ private static final String TAG = "WatchedHistoryManager";
+ private static final boolean DEBUG = false;
private static final int MAX_HISTORY_SIZE = 10000;
private static final String PREF_KEY_LAST_INDEX = "last_index";
@@ -47,8 +62,8 @@ public class WatchedHistoryManager {
new OnSharedPreferenceChangeListener() {
@Override
@MainThread
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
- String key) {
+ public void onSharedPreferenceChanged(
+ SharedPreferences sharedPreferences, String key) {
if (key.equals(PREF_KEY_LAST_INDEX)) {
final long lastIndex = mSharedPreferences.getLong(PREF_KEY_LAST_INDEX, -1);
if (lastIndex <= mLastIndex) {
@@ -57,23 +72,26 @@ public class WatchedHistoryManager {
// onSharedPreferenceChanged is always called in a main thread.
// onNewRecordAdded will be called in the same thread as the thread
// which created this instance.
- mHandler.post(new Runnable() {
- @Override
- public void run() {
- for (long i = mLastIndex + 1; i <= lastIndex; ++i) {
- WatchedRecord record = decode(
- mSharedPreferences.getString(getSharedPreferencesKey(i),
- null));
- if (record != null) {
- mWatchedHistory.add(record);
- if (mListener != null) {
- mListener.onNewRecordAdded(record);
+ mHandler.post(
+ new Runnable() {
+ @Override
+ public void run() {
+ for (long i = mLastIndex + 1; i <= lastIndex; ++i) {
+ WatchedRecord record =
+ decode(
+ mSharedPreferences.getString(
+ getSharedPreferencesKey(i),
+ null));
+ if (record != null) {
+ mWatchedHistory.add(record);
+ if (mListener != null) {
+ mListener.onNewRecordAdded(record);
+ }
+ }
}
+ mLastIndex = lastIndex;
}
- }
- mLastIndex = lastIndex;
- }
- });
+ });
}
}
};
@@ -94,9 +112,7 @@ public class WatchedHistoryManager {
mHandler = new Handler();
}
- /**
- * Starts the manager. It loads history data from {@link SharedPreferences}.
- */
+ /** Starts the manager. It loads history data from {@link SharedPreferences}. */
public void start() {
if (mStarted) {
return;
@@ -123,22 +139,22 @@ public class WatchedHistoryManager {
@WorkerThread
private void loadWatchedHistory() {
- mSharedPreferences = mContext.getSharedPreferences(
- SharedPreferencesUtils.SHARED_PREF_WATCHED_HISTORY, Context.MODE_PRIVATE);
+ mSharedPreferences =
+ mContext.getSharedPreferences(
+ SharedPreferencesUtils.SHARED_PREF_WATCHED_HISTORY, Context.MODE_PRIVATE);
mLastIndex = mSharedPreferences.getLong(PREF_KEY_LAST_INDEX, -1);
if (mLastIndex >= 0 && mLastIndex < mMaxHistorySize) {
for (int i = 0; i <= mLastIndex; ++i) {
WatchedRecord record =
- decode(mSharedPreferences.getString(getSharedPreferencesKey(i),
- null));
+ decode(mSharedPreferences.getString(getSharedPreferencesKey(i), null));
if (record != null) {
mWatchedHistory.add(record);
}
}
} else if (mLastIndex >= mMaxHistorySize) {
for (long i = mLastIndex - mMaxHistorySize + 1; i <= mLastIndex; ++i) {
- WatchedRecord record = decode(mSharedPreferences.getString(
- getSharedPreferencesKey(i), null));
+ WatchedRecord record =
+ decode(mSharedPreferences.getString(getSharedPreferencesKey(i), null));
if (record != null) {
mWatchedHistory.add(record);
}
@@ -173,9 +189,7 @@ public class WatchedHistoryManager {
return mLoaded;
}
- /**
- * Logs the record of the watched channel.
- */
+ /** Logs the record of the watched channel. */
public void logChannelViewStop(Channel channel, long endTime, long duration) {
if (duration < MIN_DURATION_MS) {
return;
@@ -185,7 +199,8 @@ public class WatchedHistoryManager {
if (DEBUG) Log.d(TAG, "Log a watched record. " + record);
mWatchedHistory.add(record);
++mLastIndex;
- mSharedPreferences.edit()
+ mSharedPreferences
+ .edit()
.putString(getSharedPreferencesKey(mLastIndex), encode(record))
.putLong(PREF_KEY_LAST_INDEX, mLastIndex)
.apply();
@@ -197,16 +212,14 @@ public class WatchedHistoryManager {
}
}
- /**
- * Sets {@link Listener}.
- */
+ /** Sets {@link Listener}. */
public void setListener(Listener listener) {
mListener = listener;
}
/**
- * Returns watched history in the ascending order of time. In other words, the first element
- * is the oldest and the last element is the latest record.
+ * Returns watched history in the ascending order of time. In other words, the first element is
+ * the oldest and the last element is the latest record.
*/
@NonNull
public List<WatchedRecord> getWatchedHistory() {
@@ -242,8 +255,12 @@ public class WatchedHistoryManager {
@Override
public String toString() {
- return "WatchedRecord: id=" + channelId + ",watchedStartTime=" + watchedStartTime
- + ",duration=" + duration;
+ return "WatchedRecord: id="
+ + channelId
+ + ",watchedStartTime="
+ + watchedStartTime
+ + ",duration="
+ + duration;
}
@Override
@@ -281,10 +298,9 @@ public class WatchedHistoryManager {
}
public interface Listener {
- /**
- * Called when history is loaded.
- */
+ /** Called when history is loaded. */
void onLoadFinished();
+
void onNewRecordAdded(WatchedRecord watchedRecord);
}
}