Home | History | Annotate | Download | only in setup
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tv.tuner.setup;
     18 
     19 import android.os.Bundle;
     20 import android.support.annotation.NonNull;
     21 import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
     22 import android.support.v17.leanback.widget.GuidedAction;
     23 import android.support.v17.leanback.widget.GuidedActionsStylist;
     24 import android.text.InputFilter;
     25 import android.text.InputFilter.AllCaps;
     26 import android.view.View;
     27 import android.widget.TextView;
     28 import com.android.tv.R;
     29 import com.android.tv.common.ui.setup.SetupGuidedStepFragment;
     30 import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
     31 import com.android.tv.tuner.util.PostalCodeUtils;
     32 import com.android.tv.util.LocationUtils;
     33 import java.util.List;
     34 
     35 /**
     36  * A fragment for initial screen.
     37  */
     38 public class PostalCodeFragment extends SetupMultiPaneFragment {
     39     public static final String ACTION_CATEGORY =
     40             "com.android.tv.tuner.setup.PostalCodeFragment";
     41     private static final int VIEW_TYPE_EDITABLE = 1;
     42 
     43     @Override
     44     protected SetupGuidedStepFragment onCreateContentFragment() {
     45         ContentFragment fragment = new ContentFragment();
     46         Bundle arguments = new Bundle();
     47         arguments.putBoolean(SetupGuidedStepFragment.KEY_THREE_PANE, true);
     48         fragment.setArguments(arguments);
     49         return fragment;
     50     }
     51 
     52     @Override
     53     protected String getActionCategory() {
     54         return ACTION_CATEGORY;
     55     }
     56 
     57     @Override
     58     protected boolean needsDoneButton() {
     59         return true;
     60     }
     61 
     62     @Override
     63     protected boolean needsSkipButton() {
     64         return true;
     65     }
     66 
     67     @Override
     68     protected void setOnClickAction(View view, final String category, final int actionId) {
     69         if (actionId == ACTION_DONE) {
     70             view.setOnClickListener(
     71                     new View.OnClickListener() {
     72                         @Override
     73                         public void onClick(View view) {
     74                             CharSequence postalCode =
     75                                     ((ContentFragment) getContentFragment()).mEditAction.getTitle();
     76                             String region = LocationUtils.getCurrentCountry(getContext());
     77                             if (postalCode != null && PostalCodeUtils.matches(postalCode, region)) {
     78                                 PostalCodeUtils.setLastPostalCode(
     79                                         getContext(), postalCode.toString());
     80                                 onActionClick(category, actionId);
     81                             } else {
     82                                 ContentFragment contentFragment =
     83                                         (ContentFragment) getContentFragment();
     84                                 contentFragment.mEditAction.setDescription(
     85                                         getString(R.string.postal_code_invalid_warning));
     86                                 contentFragment.notifyActionChanged(0);
     87                                 contentFragment.mEditedActionView.performClick();
     88                             }
     89                         }
     90                     });
     91         } else if (actionId == ACTION_SKIP) {
     92             super.setOnClickAction(view, category, ACTION_SKIP);
     93         }
     94     }
     95 
     96     public static class ContentFragment extends SetupGuidedStepFragment {
     97         private GuidedAction mEditAction;
     98         private View mEditedActionView;
     99         private View mDoneActionView;
    100         private boolean mProceed;
    101 
    102         @Override
    103         public void onGuidedActionFocused(GuidedAction action) {
    104             if (action.equals(mEditAction)) {
    105                 if (mProceed) {
    106                     // "NEXT" in IME was just clicked, moves focus to Done button.
    107                     if (mDoneActionView == null) {
    108                         mDoneActionView = getActivity().findViewById(R.id.button_done);
    109                     }
    110                     mDoneActionView.requestFocus();
    111                     mProceed = false;
    112                 } else {
    113                     // Directly opens IME to input postal/zip code.
    114                     if (mEditedActionView == null) {
    115                         int maxLength = PostalCodeUtils.getRegionMaxLength(getContext());
    116                         mEditedActionView = getView().findViewById(R.id.guidedactions_editable);
    117                         ((TextView) mEditedActionView.findViewById(R.id.guidedactions_item_title))
    118                                 .setFilters(
    119                                         new InputFilter[] {
    120                                             new InputFilter.LengthFilter(maxLength), new AllCaps()
    121                                         });
    122                     }
    123                     mEditedActionView.performClick();
    124                 }
    125             }
    126         }
    127 
    128         @Override
    129         public long onGuidedActionEditedAndProceed(GuidedAction action) {
    130             mProceed = true;
    131             return 0;
    132         }
    133 
    134         @NonNull
    135         @Override
    136         public Guidance onCreateGuidance(Bundle savedInstanceState) {
    137             String title = getString(R.string.postal_code_guidance_title);
    138             String description = getString(R.string.postal_code_guidance_description);
    139             String breadcrumb = getString(R.string.ut_setup_breadcrumb);
    140             return new Guidance(title, description, breadcrumb, null);
    141         }
    142 
    143         @Override
    144         public void onCreateActions(@NonNull List<GuidedAction> actions,
    145                 Bundle savedInstanceState) {
    146             String description = getString(R.string.postal_code_action_description);
    147             mEditAction = new GuidedAction.Builder(getActivity()).id(0).editable(true)
    148                     .description(description).build();
    149             actions.add(mEditAction);
    150         }
    151 
    152         @Override
    153         protected String getActionCategory() {
    154             return ACTION_CATEGORY;
    155         }
    156 
    157         @Override
    158         public GuidedActionsStylist onCreateActionsStylist() {
    159             return new GuidedActionsStylist() {
    160                 @Override
    161                 public int getItemViewType(GuidedAction action) {
    162                     if (action.isEditable()) {
    163                         return VIEW_TYPE_EDITABLE;
    164                     }
    165                     return super.getItemViewType(action);
    166                 }
    167 
    168                 @Override
    169                 public int onProvideItemLayoutId(int viewType) {
    170                     if (viewType == VIEW_TYPE_EDITABLE) {
    171                         return R.layout.guided_action_editable;
    172                     }
    173                     return super.onProvideItemLayoutId(viewType);
    174                 }
    175             };
    176         }
    177     }
    178 }