1 /*
2  * Copyright (C) 2018 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.settings.homepage.contextualcards;
18 
19 import static com.android.settings.homepage.contextualcards.ContextualCardsAdapter.SPAN_COUNT;
20 
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 
28 import androidx.loader.app.LoaderManager;
29 import androidx.recyclerview.widget.GridLayoutManager;
30 import androidx.recyclerview.widget.ItemTouchHelper;
31 
32 import com.android.settings.R;
33 import com.android.settings.core.InstrumentedFragment;
34 import com.android.settings.homepage.contextualcards.slices.SwipeDismissalDelegate;
35 import com.android.settings.overlay.FeatureFactory;
36 import com.android.settings.wifi.slice.ContextualWifiScanWorker;
37 
38 public class ContextualCardsFragment extends InstrumentedFragment implements
39         FocusRecyclerView.FocusListener {
40 
41     private FocusRecyclerView mCardsContainer;
42     private GridLayoutManager mLayoutManager;
43     private ContextualCardsAdapter mContextualCardsAdapter;
44     private ContextualCardManager mContextualCardManager;
45     private ItemTouchHelper mItemTouchHelper;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     public void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         final Context context = getContext();
51         if (savedInstanceState == null) {
52             FeatureFactory.getFactory(context).getSlicesFeatureProvider().newUiSession();
53         }
54         mContextualCardManager = new ContextualCardManager(context, getSettingsLifecycle(),
55                 savedInstanceState);
56 
57     }
58 
59     @Override
onStart()60     public void onStart() {
61         super.onStart();
62         ContextualWifiScanWorker.newVisibleUiSession();
63         mContextualCardManager.loadContextualCards(LoaderManager.getInstance(this));
64     }
65 
66     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)67     public View onCreateView(LayoutInflater inflater, ViewGroup container,
68             Bundle savedInstanceState) {
69         final Context context = getContext();
70         final View rootView = inflater.inflate(R.layout.settings_homepage, container, false);
71         mCardsContainer = rootView.findViewById(R.id.card_container);
72         mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT,
73                 GridLayoutManager.VERTICAL, false /* reverseLayout */);
74         mCardsContainer.setLayoutManager(mLayoutManager);
75         mContextualCardsAdapter = new ContextualCardsAdapter(context, this /* lifecycleOwner */,
76                 mContextualCardManager);
77         mCardsContainer.setAdapter(mContextualCardsAdapter);
78         mContextualCardManager.setListener(mContextualCardsAdapter);
79         mCardsContainer.setListener(this);
80         mItemTouchHelper = new ItemTouchHelper(new SwipeDismissalDelegate(mContextualCardsAdapter));
81         mItemTouchHelper.attachToRecyclerView(mCardsContainer);
82 
83         return rootView;
84     }
85 
86     @Override
onWindowFocusChanged(boolean hasWindowFocus)87     public void onWindowFocusChanged(boolean hasWindowFocus) {
88         mContextualCardManager.onWindowFocusChanged(hasWindowFocus);
89     }
90 
91     @Override
getMetricsCategory()92     public int getMetricsCategory() {
93         return SettingsEnums.SETTINGS_HOMEPAGE;
94     }
95 }
96