aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/tuner/setup/PostalCodeFragment.java
blob: 025b9193cfe783e254369ff65fae13fa36cf0af2 (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
/*
 * 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.tuner.setup;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
import android.support.v17.leanback.widget.GuidedAction;
import android.support.v17.leanback.widget.GuidedActionsStylist;
import android.text.InputFilter;
import android.text.InputFilter.AllCaps;
import android.view.View;
import android.widget.TextView;
import com.android.tv.R;
import com.android.tv.common.ui.setup.SetupGuidedStepFragment;
import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
import com.android.tv.tuner.util.PostalCodeUtils;
import com.android.tv.util.LocationUtils;
import java.util.List;

/**
 * A fragment for initial screen.
 */
public class PostalCodeFragment extends SetupMultiPaneFragment {
    public static final String ACTION_CATEGORY =
            "com.android.tv.tuner.setup.PostalCodeFragment";
    private static final int VIEW_TYPE_EDITABLE = 1;

    @Override
    protected SetupGuidedStepFragment onCreateContentFragment() {
        ContentFragment fragment = new ContentFragment();
        Bundle arguments = new Bundle();
        arguments.putBoolean(SetupGuidedStepFragment.KEY_THREE_PANE, true);
        fragment.setArguments(arguments);
        return fragment;
    }

    @Override
    protected String getActionCategory() {
        return ACTION_CATEGORY;
    }

    @Override
    protected boolean needsDoneButton() {
        return true;
    }

    @Override
    protected boolean needsSkipButton() {
        return true;
    }

    @Override
    protected void setOnClickAction(View view, final String category, final int actionId) {
        if (actionId == ACTION_DONE) {
            view.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            CharSequence postalCode =
                                    ((ContentFragment) getContentFragment()).mEditAction.getTitle();
                            String region = LocationUtils.getCurrentCountry(getContext());
                            if (postalCode != null && PostalCodeUtils.matches(postalCode, region)) {
                                PostalCodeUtils.setLastPostalCode(
                                        getContext(), postalCode.toString());
                                onActionClick(category, actionId);
                            } else {
                                ContentFragment contentFragment =
                                        (ContentFragment) getContentFragment();
                                contentFragment.mEditAction.setDescription(
                                        getString(R.string.postal_code_invalid_warning));
                                contentFragment.notifyActionChanged(0);
                                contentFragment.mEditedActionView.performClick();
                            }
                        }
                    });
        } else if (actionId == ACTION_SKIP) {
            super.setOnClickAction(view, category, ACTION_SKIP);
        }
    }

    public static class ContentFragment extends SetupGuidedStepFragment {
        private GuidedAction mEditAction;
        private View mEditedActionView;
        private View mDoneActionView;
        private boolean mProceed;

        @Override
        public void onGuidedActionFocused(GuidedAction action) {
            if (action.equals(mEditAction)) {
                if (mProceed) {
                    // "NEXT" in IME was just clicked, moves focus to Done button.
                    if (mDoneActionView == null) {
                        mDoneActionView = getActivity().findViewById(R.id.button_done);
                    }
                    mDoneActionView.requestFocus();
                    mProceed = false;
                } else {
                    // Directly opens IME to input postal/zip code.
                    if (mEditedActionView == null) {
                        int maxLength = PostalCodeUtils.getRegionMaxLength(getContext());
                        mEditedActionView = getView().findViewById(R.id.guidedactions_editable);
                        ((TextView) mEditedActionView.findViewById(R.id.guidedactions_item_title))
                                .setFilters(
                                        new InputFilter[] {
                                            new InputFilter.LengthFilter(maxLength), new AllCaps()
                                        });
                    }
                    mEditedActionView.performClick();
                }
            }
        }

        @Override
        public long onGuidedActionEditedAndProceed(GuidedAction action) {
            mProceed = true;
            return 0;
        }

        @NonNull
        @Override
        public Guidance onCreateGuidance(Bundle savedInstanceState) {
            String title = getString(R.string.postal_code_guidance_title);
            String description = getString(R.string.postal_code_guidance_description);
            String breadcrumb = getString(R.string.ut_setup_breadcrumb);
            return new Guidance(title, description, breadcrumb, null);
        }

        @Override
        public void onCreateActions(@NonNull List<GuidedAction> actions,
                Bundle savedInstanceState) {
            String description = getString(R.string.postal_code_action_description);
            mEditAction = new GuidedAction.Builder(getActivity()).id(0).editable(true)
                    .description(description).build();
            actions.add(mEditAction);
        }

        @Override
        protected String getActionCategory() {
            return ACTION_CATEGORY;
        }

        @Override
        public GuidedActionsStylist onCreateActionsStylist() {
            return new GuidedActionsStylist() {
                @Override
                public int getItemViewType(GuidedAction action) {
                    if (action.isEditable()) {
                        return VIEW_TYPE_EDITABLE;
                    }
                    return super.getItemViewType(action);
                }

                @Override
                public int onProvideItemLayoutId(int viewType) {
                    if (viewType == VIEW_TYPE_EDITABLE) {
                        return R.layout.guided_action_editable;
                    }
                    return super.onProvideItemLayoutId(viewType);
                }
            };
        }
    }
}