1 /*
2 
3 * Copyright (C) 2017 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License
16 */
17 
18 package com.android.dialer.searchfragment.directories;
19 
20 import android.content.Context;
21 import android.content.CursorLoader;
22 import android.database.Cursor;
23 import android.provider.ContactsContract;
24 import android.support.annotation.Nullable;
25 import com.android.dialer.common.LogUtil;
26 import com.android.dialer.util.PermissionsUtil;
27 import com.google.auto.value.AutoValue;
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * {@link CursorLoader} to load information about all directories (local and remote).
33  *
34  * <p>Information about a directory includes its ID, display name, etc, but doesn't include the
35  * contacts in it.
36  */
37 public final class DirectoriesCursorLoader extends CursorLoader {
38 
39   public static final String[] PROJECTION = {
40     ContactsContract.Directory._ID,
41     ContactsContract.Directory.DISPLAY_NAME,
42     ContactsContract.Directory.PHOTO_SUPPORT,
43   };
44 
45   // Indices of columns in PROJECTION
46   private static final int ID = 0;
47   private static final int DISPLAY_NAME = 1;
48   private static final int PHOTO_SUPPORT = 2;
49 
DirectoriesCursorLoader(Context context)50   public DirectoriesCursorLoader(Context context) {
51     super(
52         context,
53         ContactsContract.Directory.ENTERPRISE_CONTENT_URI,
54         PROJECTION,
55         null,
56         null,
57         ContactsContract.Directory._ID);
58   }
59 
60   @Override
loadInBackground()61   public Cursor loadInBackground() {
62     if (!PermissionsUtil.hasContactsReadPermissions(getContext())) {
63       LogUtil.i("DirectoriesCursorLoader.loadInBackground", "Contacts permission denied.");
64       return null;
65     }
66     return super.loadInBackground();
67   }
68 
69   /**
70    * Creates a complete list of directories from the data set loaded by this loader.
71    *
72    * @param cursor A cursor pointing to the data set loaded by this loader. The caller must ensure
73    *     the cursor is not null.
74    * @return A list of directories.
75    */
toDirectories(Cursor cursor)76   public static List<Directory> toDirectories(Cursor cursor) {
77     if (cursor == null) {
78       LogUtil.i("DirectoriesCursorLoader.toDirectories", "Cursor was null");
79       return new ArrayList<>();
80     }
81 
82     List<Directory> directories = new ArrayList<>();
83     cursor.moveToPosition(-1);
84     while (cursor.moveToNext()) {
85       directories.add(
86           Directory.create(
87               cursor.getInt(ID),
88               cursor.getString(DISPLAY_NAME),
89               /* supportsPhotos = */ cursor.getInt(PHOTO_SUPPORT) != 0));
90     }
91     return directories;
92   }
93 
94   /** POJO representing the results returned from {@link DirectoriesCursorLoader}. */
95   @AutoValue
96   public abstract static class Directory {
create(long id, @Nullable String displayName, boolean supportsPhotos)97     public static Directory create(long id, @Nullable String displayName, boolean supportsPhotos) {
98       return new AutoValue_DirectoriesCursorLoader_Directory(id, displayName, supportsPhotos);
99     }
100 
getId()101     public abstract long getId();
102 
103     /** Returns a user facing display name of the directory. Null if none exists. */
getDisplayName()104     public abstract @Nullable String getDisplayName();
105 
supportsPhotos()106     public abstract boolean supportsPhotos();
107   }
108 }
109