aboutsummaryrefslogtreecommitdiff
path: root/partner_support/samples/src/com/example/partnersupportsampletvinput/LineupSelectionFragment.java
blob: 8c3ca77a8b28ef45d20916be8d81efabcb0cfaba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * 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.example.partnersupportsampletvinput;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v17.leanback.app.GuidedStepFragment;
import android.support.v17.leanback.widget.GuidanceStylist;
import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
import android.support.v17.leanback.widget.GuidedAction;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.tv.partner.support.EpgContract;
import com.google.android.tv.partner.support.EpgInput;
import com.google.android.tv.partner.support.EpgInputs;
import com.google.android.tv.partner.support.Lineup;
import com.google.android.tv.partner.support.Lineups;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/** Lineup Selection Fragment for users to select a lineup */
public class LineupSelectionFragment extends GuidedStepFragment {
    public static final boolean DEBUG = false;
    public static final String TAG = "LineupSelectionFragment";

    public static final String KEY_TITLE = "title";
    public static final String KEY_DESCRIPTION = "description";
    private static final long ACTION_ID_STATUS = 1;
    private static final long ACTION_ID_LINEUPS = 2;
    private static final Pattern CHANNEL_NUMBER_DELIMITER = Pattern.compile("([ .-])");
    private Activity mActivity;
    private GuidedAction mStatusAction;
    private String mPostcode;
    private List<String> mChannelNumbers;
    private Map<GuidedAction, Lineup> mActionLineupMap = new HashMap<>();

    private AsyncTask<Void, Void, List<Pair<Lineup, Integer>>> mFetchLineupTask;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        mActivity = getActivity();
        mStatusAction =
                new GuidedAction.Builder(mActivity)
                        .id(ACTION_ID_STATUS)
                        .title("Loading lineups")
                        .description("please wait")
                        .focusable(false)
                        .build();

        mFetchLineupTask =
                new AsyncTask<Void, Void, List<Pair<Lineup, Integer>>>() {
                    @Override
                    protected List<Pair<Lineup, Integer>> doInBackground(Void... voids) {
                        return lineupChannelMatchCount(getLineups(), getChannelNumbers());
                    }

                    @Override
                    protected void onPostExecute(List<Pair<Lineup, Integer>> result) {
                        List<GuidedAction> actions = new ArrayList<>();
                        if (result.isEmpty()) {
                            mStatusAction.setTitle("No lineup found for postcode: " + mPostcode);
                            mStatusAction.setDescription("");
                            mStatusAction.setFocusable(true);
                            notifyActionChanged(findActionPositionById(ACTION_ID_STATUS));
                            return;
                        }
                        mActionLineupMap = new HashMap<>();
                        for (Pair<Lineup, Integer> pair : result) {
                            Lineup lineup = pair.first;
                            String title =
                                    TextUtils.isEmpty(lineup.getName())
                                            ? lineup.getId()
                                            : lineup.getName();
                            GuidedAction action =
                                    new GuidedAction.Builder(getActivity())
                                            .id(ACTION_ID_LINEUPS)
                                            .title(title)
                                            .description(pair.second + " channels match")
                                            .build();
                            mActionLineupMap.put(action, lineup);
                            actions.add(action);
                        }
                        setActions(actions);
                    }
                };

        super.onCreate(savedInstanceState);
    }

    @NonNull
    @Override
    public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
        return new Guidance(
                "LineupSelectionFragment", "LineupSelectionFragment Description", null, null);
    }

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, savedInstanceState);
        if (!mFetchLineupTask.isCancelled()) {
            mPostcode = getArguments().getString(ChannelScanFragment.KEY_POSTCODE);
            mChannelNumbers =
                    getArguments().getStringArrayList(ChannelScanFragment.KEY_CHANNEL_NUMBERS);
            mFetchLineupTask.execute();
        }
        return v;
    }

    @Override
    public void onDestroyView() {
        mFetchLineupTask.cancel(true);
        super.onDestroyView();
    }

    @Override
    public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
        actions.add(mStatusAction);
    }

    @Override
    public void onGuidedActionClicked(GuidedAction action) {
        if (action == null) {
            return;
        }
        switch ((int) action.getId()) {
            case (int) ACTION_ID_STATUS:
                finishGuidedStepFragments();
                break;
            case (int) ACTION_ID_LINEUPS:
                FragmentManager fm = getFragmentManager();
                super.onGuidedActionClicked(action);
                onLineupSelected(mActionLineupMap.get(action));
                ResultFragment fragment = new ResultFragment();
                Bundle args = new Bundle();
                args.putString(KEY_TITLE, action.getTitle().toString());
                args.putString(KEY_DESCRIPTION, action.getDescription().toString());
                fragment.setArguments(args);
                GuidedStepFragment.add(fm, fragment);
                break;
            default:
        }
    }

    private List<Pair<Lineup, Integer>> lineupChannelMatchCount(
            List<Lineup> lineups, List<String> localChannels) {
        List<Pair<Lineup, Integer>> result = new ArrayList<>();
        for (Lineup lineup : lineups) {
            result.add(new Pair<>(lineup, getMatchCount(lineup.getChannels(), localChannels)));
        }
        // sort in decreasing order
        Collections.sort(
                result,
                new Comparator<Pair<Lineup, Integer>>() {
                    @Override
                    public int compare(Pair<Lineup, Integer> pair, Pair<Lineup, Integer> other) {
                        return Integer.compare(other.second, pair.second);
                    }
                });
        return result;
    }

    private static int getMatchCount(List<String> lineupChannels, List<String> localChannels) {
        int count = 0;
        for (String lineupChannel : lineupChannels) {
            if (TextUtils.isEmpty(lineupChannel)) {
                continue;
            }
            List<String> parsedNumbers = parseChannelNumber(lineupChannel);
            for (String channel : localChannels) {
                if (TextUtils.isEmpty(channel)) {
                    continue;
                }
                if (matchChannelNumber(parsedNumbers, parseChannelNumber(channel))) {
                    if (DEBUG) {
                        Log.d(TAG, lineupChannel + " matches " + channel);
                    }
                    count++;
                    break;
                }
            }
        }
        return count;
    }

    private List<Lineup> getLineups() {
        return new ArrayList<>(Lineups.query(mActivity.getContentResolver(), mPostcode));
    }

    private List<String> getChannelNumbers() {
        return mChannelNumbers;
    }

    private void onLineupSelected(@Nullable Lineup lineup) {
        if (lineup == null) {
            return;
        }
        ContentValues values = new ContentValues();
        values.put(EpgContract.EpgInputs.COLUMN_INPUT_ID, SampleTvInputService.INPUT_ID);
        values.put(EpgContract.EpgInputs.COLUMN_LINEUP_ID, lineup.getId());

        ContentResolver contentResolver = getActivity().getContentResolver();
        EpgInput epgInput = EpgInputs.queryEpgInput(contentResolver, SampleTvInputService.INPUT_ID);
        if (epgInput == null) {
            contentResolver.insert(EpgContract.EpgInputs.CONTENT_URI, values);
        } else {
            values.put(EpgContract.EpgInputs.COLUMN_ID, epgInput.getId());
            EpgInputs.update(contentResolver, EpgInput.createEpgChannel(values));
        }
    }

    /**
     * Parses the channel number string to a list of numbers (major number, minor number, etc.).
     *
     * @param channelNumber the display number of the channel
     * @return a list of numbers
     */
    private static List<String> parseChannelNumber(String channelNumber) {
        // TODO: add tests for this method
        List<String> numbers =
                new ArrayList<>(
                        Arrays.asList(TextUtils.split(channelNumber, CHANNEL_NUMBER_DELIMITER)));
        numbers.removeAll(Collections.singleton(""));
        if (numbers.size() < 1 || numbers.size() > 2) {
            Log.w(TAG, "unsupported channel number format: " + channelNumber);
            return new ArrayList<>();
        }
        return numbers;
    }

    /**
     * Checks whether two lists of channel numbers match or not. If the sizes are different,
     * additional elements are ignore.
     */
    private static boolean matchChannelNumber(List<String> numbers, List<String> other) {
        if (numbers.isEmpty() || other.isEmpty()) {
            return false;
        }
        int i = 0;
        int j = 0;
        while (i < numbers.size() && j < other.size()) {
            if (!numbers.get(i++).equals(other.get(j++))) {
                return false;
            }
        }
        return true;
    }
}