1 /*
2  * Copyright (C) 2020 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.notification.app;
18 
19 import android.app.people.IPeopleManager;
20 import android.content.Context;
21 import android.os.AsyncTask;
22 import android.os.RemoteException;
23 import android.service.notification.ConversationChannelWrapper;
24 import android.util.Log;
25 import android.view.View;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.notification.NotificationBackend;
31 import com.android.settingslib.widget.LayoutPreference;
32 
33 public class NoConversationsPreferenceController extends ConversationListPreferenceController {
34 
35     private static String TAG = "NoConversationsPC";
36     private static final String KEY = "no_conversations";
37 
38     private IPeopleManager mPs;
39     private int mConversationCount = 0;
40 
NoConversationsPreferenceController(Context context, NotificationBackend backend, IPeopleManager ps)41     public NoConversationsPreferenceController(Context context,
42             NotificationBackend backend, IPeopleManager ps) {
43         super(context, backend);
44         mPs = ps;
45     }
46 
47     @Override
getPreferenceKey()48     public String getPreferenceKey() {
49         return KEY;
50     }
51 
52     @Override
isAvailable()53     public boolean isAvailable() {
54         return true;
55     }
56 
57     @Override
getSummaryPreference()58     Preference getSummaryPreference() {
59         return null;
60     }
61 
62     @Override
matchesFilter(ConversationChannelWrapper conversation)63     boolean matchesFilter(ConversationChannelWrapper conversation) {
64         return false;
65     }
66 
67     @Override
updateState(Preference preference)68     public void updateState(Preference preference) {
69         LayoutPreference pref = (LayoutPreference) preference;
70         // Load conversations
71         new AsyncTask<Void, Void, Void>() {
72             @Override
73             protected Void doInBackground(Void... unused) {
74                 mConversationCount = mBackend.getConversations(false).getList().size();
75                 try {
76                     mConversationCount += mPs.getRecentConversations().getList().size();
77                 } catch (RemoteException e) {
78                     Log.w(TAG, "Error calling PS", e);
79                 }
80                 return null;
81             }
82 
83             @Override
84             protected void onPostExecute(Void unused) {
85                 if (mContext == null) {
86                     return;
87                 }
88                 pref.findViewById(R.id.onboarding).setVisibility(mConversationCount == 0
89                         ? View.VISIBLE : View.GONE);
90                 preference.setVisible(mConversationCount == 0);
91             }
92         }.execute();
93     }
94 }
95