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.example.android.multidisplay.launcher;
18 
19 import android.os.Bundle;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.GridView;
24 
25 import com.example.android.multidisplay.R;
26 
27 import androidx.fragment.app.DialogFragment;
28 
29 /**
30  * Callback to be invoked when an app was picked.
31  */
32 interface AppPickedCallback {
onAppPicked(AppEntry appEntry)33     void onAppPicked(AppEntry appEntry);
34 }
35 
36 /**
37  * Dialog that provides the user with a list of available apps to pin to the home screen.
38  */
39 public class PinnedAppPickerDialog extends DialogFragment {
40 
41     private AppListAdapter mAppListAdapter;
42     private AppPickedCallback mAppPickerCallback;
43 
PinnedAppPickerDialog()44     public PinnedAppPickerDialog() {
45     }
46 
newInstance(AppListAdapter appListAdapter, AppPickedCallback callback)47     public static PinnedAppPickerDialog newInstance(AppListAdapter appListAdapter,
48             AppPickedCallback callback) {
49         PinnedAppPickerDialog frag = new PinnedAppPickerDialog();
50         frag.mAppListAdapter = appListAdapter;
51         frag.mAppPickerCallback = callback;
52         return frag;
53     }
54 
55     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)56     public View onCreateView(LayoutInflater inflater, ViewGroup container,
57             Bundle savedInstanceState) {
58         return inflater.inflate(R.layout.app_picker_dialog, container);
59     }
60 
61     @Override
onViewCreated(View view, Bundle savedInstanceState)62     public void onViewCreated(View view, Bundle savedInstanceState) {
63         super.onViewCreated(view, savedInstanceState);
64 
65         GridView appGridView = view.findViewById(R.id.picker_app_grid);
66         appGridView.setAdapter(mAppListAdapter);
67         appGridView.setOnItemClickListener((adapterView, itemView, position, id) -> {
68             final AppEntry entry = mAppListAdapter.getItem(position);
69             mAppPickerCallback.onAppPicked(entry);
70             dismiss();
71         });
72     }
73 }