1 /*
2  * Copyright (C) 2016 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.permissioncontroller.permission.ui.handheld;
18 
19 import static com.android.permissioncontroller.Constants.EXTRA_SESSION_ID;
20 import static com.android.permissioncontroller.permission.ui.handheld.UtilsKt.pressBack;
21 
22 import android.os.Bundle;
23 import android.view.MenuItem;
24 
25 import androidx.lifecycle.ViewModelProvider;
26 
27 import com.android.permissioncontroller.permission.ui.model.ManageCustomPermissionsViewModel;
28 import com.android.permissioncontroller.permission.ui.model.ManageCustomPermissionsViewModelFactory;
29 
30 import java.util.HashMap;
31 
32 /**
33  * Fragment that allows the user to manage custom permissions.
34  */
35 public class ManageCustomPermissionsFragment extends ManagePermissionsFragment {
36 
37     private ManageCustomPermissionsViewModel mViewModel;
38 
39     /**
40      * Create a bundle with the arguments needed by this fragment
41      *
42      * @param sessionId The current session ID
43      * @return A bundle with all of the args placed
44      */
createArgs(long sessionId)45     public static Bundle createArgs(long sessionId) {
46         Bundle arguments = new Bundle();
47         arguments.putLong(EXTRA_SESSION_ID, sessionId);
48         return arguments;
49     }
50 
51     @Override
onCreate(Bundle icicle)52     public void onCreate(Bundle icicle) {
53         super.onCreate(icicle);
54 
55         ManageCustomPermissionsViewModelFactory factory =
56                 new ManageCustomPermissionsViewModelFactory(getActivity().getApplication());
57         mViewModel = new ViewModelProvider(this, factory)
58                 .get(ManageCustomPermissionsViewModel.class);
59         mPermissionGroups = mViewModel.getUiDataLiveData().getValue();
60 
61         mViewModel.getUiDataLiveData().observe(this, permissionGroups -> {
62             if (permissionGroups == null) {
63                 mPermissionGroups = new HashMap<>();
64             } else {
65                 mPermissionGroups = permissionGroups;
66             }
67             updatePermissionsUi();
68         });
69     }
70 
71     @Override
showPermissionApps(String permissionGroupName)72     public void showPermissionApps(String permissionGroupName) {
73         mViewModel.showPermissionApps(this, PermissionAppsFragment.createArgs(
74                 permissionGroupName, getArguments().getLong(EXTRA_SESSION_ID)));
75     }
76 
77     @Override
onStart()78     public void onStart() {
79         super.onStart();
80 
81         getActivity().setTitle(com.android.permissioncontroller.R.string.additional_permissions);
82     }
83 
84     @Override
onOptionsItemSelected(MenuItem item)85     public boolean onOptionsItemSelected(MenuItem item) {
86         switch (item.getItemId()) {
87             case android.R.id.home:
88                 pressBack(this);
89                 return true;
90         }
91         return super.onOptionsItemSelected(item);
92     }
93 }
94