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 17package android.support.wear.widget; 18 19import android.os.Bundle; 20import android.preference.PreferenceFragment; 21import android.support.wear.widget.SwipeDismissFrameLayout.Callback; 22import android.util.TypedValue; 23import android.view.LayoutInflater; 24import android.view.View; 25import android.view.ViewGroup; 26 27/** 28 * {@link PreferenceFragment} that supports swipe-to-dismiss. 29 * 30 * <p>Unlike a regular PreferenceFragment, this Fragment has a solid color background using the 31 * background color from the theme. This allows the fragment to be layered on top of other 32 * fragments so that the previous layer is seen when this fragment is swiped away. 33 */ 34public class SwipeDismissPreferenceFragment extends PreferenceFragment { 35 36 private SwipeDismissFrameLayout mSwipeLayout; 37 38 private final Callback mCallback = 39 new Callback() { 40 @Override 41 public void onSwipeStarted(SwipeDismissFrameLayout layout) { 42 SwipeDismissPreferenceFragment.this.onSwipeStart(); 43 } 44 45 @Override 46 public void onSwipeCanceled(SwipeDismissFrameLayout layout) { 47 SwipeDismissPreferenceFragment.this.onSwipeCancelled(); 48 } 49 50 @Override 51 public void onDismissed(SwipeDismissFrameLayout layout) { 52 SwipeDismissPreferenceFragment.this.onDismiss(); 53 } 54 }; 55 56 @Override 57 public View onCreateView( 58 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 59 60 mSwipeLayout = new SwipeDismissFrameLayout(getActivity()); 61 mSwipeLayout.addCallback(mCallback); 62 63 View contents = super.onCreateView(inflater, mSwipeLayout, savedInstanceState); 64 65 mSwipeLayout.setBackgroundColor(getBackgroundColor()); 66 mSwipeLayout.addView(contents); 67 68 return mSwipeLayout; 69 } 70 71 /** Called when the fragment is dismissed with a swipe. */ 72 public void onDismiss() { 73 } 74 75 /** Called when a swipe-to-dismiss gesture is started. */ 76 public void onSwipeStart() { 77 } 78 79 /** Called when a swipe-to-dismiss gesture is cancelled. */ 80 public void onSwipeCancelled() { 81 } 82 83 /** 84 * Sets whether or not the preferences list can be focused. If {@code focusable} is false, any 85 * existing focus will be cleared. 86 */ 87 public void setFocusable(boolean focusable) { 88 if (focusable) { 89 mSwipeLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); 90 mSwipeLayout.setFocusable(true); 91 } else { 92 // Prevent any child views from receiving focus. 93 mSwipeLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); 94 95 mSwipeLayout.setFocusable(false); 96 mSwipeLayout.clearFocus(); 97 } 98 } 99 100 private int getBackgroundColor() { 101 TypedValue value = new TypedValue(); 102 getActivity().getTheme().resolveAttribute(android.R.attr.colorBackground, value, true); 103 return value.data; 104 } 105} 106