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.settings.connectivity.setup; 18 19 import android.annotation.Nullable; 20 import android.app.Activity; 21 import android.content.res.Resources; 22 import android.os.Bundle; 23 import android.support.v17.leanback.app.GuidedStepSupportFragment; 24 import android.support.v17.leanback.widget.GuidanceStylist; 25 import android.support.v17.leanback.widget.GuidedActionsStylist; 26 import android.support.v17.leanback.widget.VerticalGridView; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.ViewGroup; 30 31 import com.android.tv.settings.R; 32 import com.android.tv.settings.connectivity.util.GuidedActionsAlignUtil; 33 34 35 /** 36 * Subclass of {@link GuidedStepSupportFragment} used in settings wifi setup. 37 */ 38 public class WifiConnectivityGuidedStepFragment extends GuidedStepSupportFragment { 39 40 @Override 41 protected void onProvideFragmentTransitions() { 42 setEnterTransition(null); 43 setExitTransition(null); 44 } 45 46 @Override 47 public GuidanceStylist onCreateGuidanceStylist() { 48 return new GuidanceStylist() { 49 @Override 50 public int onProvideLayoutId() { 51 return R.layout.wifi_content; 52 } 53 }; 54 } 55 56 @Override 57 public GuidedActionsStylist onCreateActionsStylist() { 58 return new GuidedActionsStylist() { 59 @Override 60 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 61 LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 62 View v = inflater.inflate(onProvideItemLayoutId(viewType), parent, false); 63 return new GuidedActionsAlignUtil.SetupViewHolder(v); 64 } 65 }; 66 } 67 68 /** 69 * Get resources safely. 70 * 71 * @return resources. 72 */ 73 @Nullable 74 public Resources getResourcesSafely() { 75 final Activity activity = getActivity(); 76 if (activity == null || activity.isFinishing()) { 77 return null; 78 } 79 return activity.getResources(); 80 } 81 82 @Override 83 public View onCreateView(LayoutInflater inflater, ViewGroup container, 84 Bundle savedInstanceState) { 85 View view = super.onCreateView(inflater, container, savedInstanceState); 86 // action_fragment_root's padding cannot be set via attributes so we do it programmatically. 87 final View actionFragmentRoot = view.findViewById(R.id.action_fragment_root); 88 if (actionFragmentRoot != null) { 89 actionFragmentRoot.setPadding(0, 0, 0, 0); 90 } 91 final Resources resources = getResourcesSafely(); 92 if (resources == null) { 93 return view; 94 } 95 96 final VerticalGridView gridView = getGuidedActionsStylist().getActionsGridView(); 97 gridView.setItemSpacing( 98 resources.getDimensionPixelSize(R.dimen.setup_list_item_margin)); 99 GuidedActionsAlignUtil.align(getGuidedActionsStylist()); 100 return view; 101 } 102 } 103