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.common.ui.setup.SetupGuidedStepFragment;
     29 import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
     30 import com.android.tv.common.util.LocationUtils;
     31 import com.android.tv.common.util.PostalCodeUtils;
     32 import com.android.tv.tuner.R;
     33 import java.util.List;
     34 
     35 /** A fragment for initial screen. */
     36 public class PostalCodeFragment extends SetupMultiPaneFragment {
     37     public static final String ACTION_CATEGORY = "com.android.tv.tuner.setup.PostalCodeFragment";
     38     public static final String KEY_POSTAL_CODE = "postal_code";
     39     private static final int VIEW_TYPE_EDITABLE = 1;
     40 
     41     @Override
     42     protected SetupGuidedStepFragment onCreateContentFragment() {
     43         ContentFragment fragment = new ContentFragment();
     44         Bundle arguments = new Bundle();
     45         arguments.putBoolean(SetupGuidedStepFragment.KEY_THREE_PANE, true);
     46         fragment.setArguments(arguments);
     47         return fragment;
     48     }
     49 
     50     @Override
     51     protected String getActionCategory() {
     52         return ACTION_CATEGORY;
     53     }
     54 
     55     @Override
     56     protected boolean needsDoneButton() {
     57         return true;
     58     }
     59 
     60     @Override
     61     protected boolean needsSkipButton() {
     62         return true;
     63     }
     64 
     65     @Override
     66     protected void setOnClickAction(View view, final String category, final int actionId) {
     67         if (actionId == ACTION_DONE) {
     68             view.setOnClickListener(
     69                     new View.OnClickListener() {
     70                         @Override
     71                         public void onClick(View view) {
     72                             CharSequence postalCode =
     73                                     ((ContentFragment) getContentFragment())
     74                                             .mEditedActionTitleView.getText();
     75                             String region = LocationUtils.getCurrentCountry(getContext());
     76                             if (postalCode != null && PostalCodeUtils.matches(postalCode, region)) {
     77                                 String postalCodeString = postalCode.toString();
     78                                 PostalCodeUtils.setLastPostalCode(getContext(), postalCodeString);
     79                                 Bundle params = new Bundle();
     80                                 params.putString(KEY_POSTAL_CODE, postalCodeString);
     81                                 onActionClick(category, actionId, params);
     82                             } else {
     83                                 ContentFragment contentFragment =
     84                                         (ContentFragment) getContentFragment();
     85                                 contentFragment.mEditAction.setDescription(
     86                                         getString(R.string.postal_code_invalid_warning));
     87                                 contentFragment.notifyActionChanged(0);
     88                                 contentFragment.mEditedActionView.performClick();
     89                             }
     90                         }
     91                     });
     92         } else if (actionId == ACTION_SKIP) {
     93             super.setOnClickAction(view, category, ACTION_SKIP);
     94         }
     95     }
     96 
     97     /** The content fragment of {@link PostalCodeFragment}. */
     98     public static class ContentFragment extends SetupGuidedStepFragment {
     99         private GuidedAction mEditAction;
    100         private View mEditedActionView;
    101         private TextView mEditedActionTitleView;
    102         private View mDoneActionView;
    103         private boolean mProceed;
    104 
    105         @Override
    106         public void onGuidedActionFocused(GuidedAction action) {
    107             if (action.equals(mEditAction)) {
    108                 if (mProceed) {
    109                     // "NEXT" in IME was just clicked, moves focus to Done button.
    110                     if (mDoneActionView == null) {
    111                         mDoneActionView = getDoneButton();
    112                     }
    113                     mDoneActionView.requestFocus();
    114                     mProceed = false;
    115                 } else {
    116                     // Directly opens IME to input postal/zip code.
    117                     if (mEditedActionView == null) {
    118                         int maxLength = PostalCodeUtils.getRegionMaxLength(getContext());
    119                         mEditedActionView = getView().findViewById(R.id.guidedactions_editable);
    120                         mEditedActionTitleView =
    121                                 mEditedActionView.findViewById(R.id.guidedactions_item_title);
    122                         mEditedActionTitleView.setFilters(
    123                                 new InputFilter[] {
    124                                     new InputFilter.LengthFilter(maxLength), new AllCaps()
    125                                 });
    126                     }
    127                     mEditedActionView.performClick();
    128                 }
    129             }
    130         }
    131 
    132         @Override
    133         public long onGuidedActionEditedAndProceed(GuidedAction action) {
    134             mProceed = true;
    135             return 0;
    136         }
    137 
    138         @NonNull
    139         @Override
    140         public Guidance onCreateGuidance(Bundle savedInstanceState) {
    141             String title = getString(R.string.postal_code_guidance_title);
    142             String description = getString(R.string.postal_code_guidance_description);
    143             String breadcrumb = getString(R.string.ut_setup_breadcrumb);
    144             return new Guidance(title, description, breadcrumb, null);
    145         }
    146 
    147         @Override
    148         public void onCreateActions(
    149                 @NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
    150             String description = getString(R.string.postal_code_action_description);
    151             mEditAction =
    152                     new GuidedAction.Builder(getActivity())
    153                             .id(0)
    154                             .editable(true)
    155                             .description(description)
    156                             .build();
    157             actions.add(mEditAction);
    158         }
    159 
    160         @Override
    161         protected String getActionCategory() {
    162             return ACTION_CATEGORY;
    163         }
    164 
    165         @Override
    166         public GuidedActionsStylist onCreateActionsStylist() {
    167             return new GuidedActionsStylist() {
    168                 @Override
    169                 public int getItemViewType(GuidedAction action) {
    170                     if (action.isEditable()) {
    171                         return VIEW_TYPE_EDITABLE;
    172                     }
    173                     return super.getItemViewType(action);
    174                 }
    175 
    176                 @Override
    177                 public int onProvideItemLayoutId(int viewType) {
    178                     if (viewType == VIEW_TYPE_EDITABLE) {
    179                         return R.layout.guided_action_editable;
    180                     }
    181                     return super.onProvideItemLayoutId(viewType);
    182                 }
    183             };
    184         }
    185     }
    186 }
    187