summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/lookup/LookupCache.java
blob: e6d5cc697d8aa2eb8731a9a343183b5be10b6a03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright (C) 2014 Xiao-Long Chen <chenxiaolong@cxl.epac.to>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.lookup;

import com.android.dialer.phonenumbercache.ContactInfo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract.Contacts;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.util.JsonReader;
import android.util.JsonWriter;
import android.util.Log;

import com.android.dialer.util.DialerUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

public class LookupCache {
    private static final String TAG = LookupCache.class.getSimpleName();

    public static final String NAME = "Name";
    public static final String TYPE = "Type";
    public static final String LABEL = "Label";
    public static final String NUMBER = "Number";
    public static final String FORMATTED_NUMBER = "FormattedNumber";
    public static final String NORMALIZED_NUMBER = "NormalizedNumber";
    public static final String PHOTO_ID = "PhotoID";
    //public static final String PHOTO_URI = "PhotoURI";
    public static final String LOOKUP_URI = "LookupURI";

    public static boolean hasCachedContact(Context context, String number) {
        String normalizedNumber = formatE164(context, number);

        if (normalizedNumber == null) {
            return false;
        }

        File file = getFilePath(context, normalizedNumber);
        return file.exists();
    }

    public static void cacheContact(Context context, ContactInfo info) {
        File file = getFilePath(context, info.normalizedNumber);

        if (file.exists()) {
            file.delete();
        }

        FileOutputStream out = null;
        JsonWriter writer = null;

        try {
            out = new FileOutputStream(file);
            writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
            writer.setIndent("  ");
            List messages = new ArrayList();

            writer.beginObject();
            if (info.name != null) writer.name(NAME).value(info.name);
            writer.name(TYPE).value(info.type);
            if (info.label != null) writer.name(LABEL).value(info.label);
            if (info.number != null) writer.name(NUMBER).value(info.number);
            if (info.formattedNumber != null) {
                writer.name(FORMATTED_NUMBER).value(info.formattedNumber);
            }
            if (info.normalizedNumber != null) {
                writer.name(NORMALIZED_NUMBER).value(info.normalizedNumber);
            }
            writer.name(PHOTO_ID).value(info.photoId);

            if (info.lookupUri != null) {
                writer.name(LOOKUP_URI).value(info.lookupUri.toString());
            }

            // We do not save the photo URI. If there's a cached image, that
            // will be used when the contact is retrieved. Otherwise, photoUri
            // will be set to null.

            writer.endObject();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            DialerUtils.closeQuietly(writer);
            DialerUtils.closeQuietly(out);
        }
    }

    public static ContactInfo getCachedContact(Context context, String number) {
        String normalizedNumber = formatE164(context, number);

        if (normalizedNumber == null) {
            return null;
        }

        File file = getFilePath(context, normalizedNumber);
        if (!file.exists()) {
            // Whatever is calling this should probably check anyway
            return null;
        }

        ContactInfo info = new ContactInfo();

        FileInputStream in = null;
        JsonReader reader = null;

        try {
            in = new FileInputStream(file);
            reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();

                if (NAME.equals(name)) {
                    info.name = reader.nextString();
                } else if (TYPE.equals(name)) {
                    info.type = reader.nextInt();
                } else if (LABEL.equals(name)) {
                    info.label = reader.nextString();
                } else if (NUMBER.equals(name)) {
                    info.number = reader.nextString();
                } else if (FORMATTED_NUMBER.equals(name)) {
                    info.formattedNumber = reader.nextString();
                } else if (NORMALIZED_NUMBER.equals(name)) {
                    info.normalizedNumber = reader.nextString();
                } else if (PHOTO_ID.equals(name)) {
                    info.photoId = reader.nextInt();
                } else if (LOOKUP_URI.equals(name)) {
                    Uri lookupUri = Uri.parse(reader.nextString());

                    if (hasCachedImage(context, normalizedNumber)) {
                        // Insert cached photo URI
                        Uri image = Uri.withAppendedPath(
                                LookupProvider.IMAGE_CACHE_URI,
                                Uri.encode(normalizedNumber));

                        String json = lookupUri.getEncodedFragment();
                        if (json != null) {
                            try {
                                JSONObject jsonObj = new JSONObject(json);
                                jsonObj.putOpt(Contacts.PHOTO_URI, image.toString());
                                lookupUri = lookupUri.buildUpon()
                                        .encodedFragment(jsonObj.toString())
                                        .build();
                            } catch (JSONException e) {
                                Log.e(TAG, "Failed to add image URI to json", e);
                            }
                        }

                        info.photoUri = image;
                    }

                    info.lookupUri = lookupUri;
                }
            }
            reader.endObject();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            DialerUtils.closeQuietly(reader);
            DialerUtils.closeQuietly(in);
        }

        return info;
    }

    public static void deleteCachedContacts(Context context) {
        File dir = new File(context.getCacheDir()
                + File.separator + "lookup");

        if (!dir.exists()) {
            Log.v(TAG, "Lookup cache directory does not exist. Not clearing it.");
            return;
        }

        if (!dir.isDirectory()) {
            Log.e(TAG, "Path " + dir + " is not a directory");
            return;
        }

        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    file.delete();
                }
            }
        }
    }

    public static void deleteCachedContact(
            Context context, String normalizedNumber) {
        File f = getFilePath(context, normalizedNumber);
        if (f.exists()) {
            f.delete();
        }

        f = getImagePath(context, normalizedNumber);
        if (f.exists()) {
            f.delete();
        }
    }

    public static boolean hasCachedImage(Context context, String number) {
        String normalizedNumber = formatE164(context, number);

        if (normalizedNumber == null) {
            return false;
        }

        File file = getImagePath(context, normalizedNumber);
        return file.exists();
    }

    public static void cacheImage(Context context,
            String normalizedNumber, Bitmap bmp) {
        // Compress the cached images to save space
        if (bmp == null) {
            Log.e(TAG, "Failed to cache image");
            return;
        }

        File image = getImagePath(context, normalizedNumber);

        FileOutputStream out = null;

        try {
            out = new FileOutputStream(image);
            bmp.compress(Bitmap.CompressFormat.WEBP, 100, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DialerUtils.closeQuietly(out);
        }
    }

    public static Bitmap getCachedImage(Context context, String normalizedNumber) {
        File image = getImagePath(context, normalizedNumber);
        if (!image.exists()) {
            return null;
        }

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeFile(image.getPath(), options);
    }

    private static String formatE164(Context context, String number) {
        String countryIso = ((TelephonyManager) context.getSystemService(
                Context.TELEPHONY_SERVICE)).getSimCountryIso().toUpperCase();
        return PhoneNumberUtils.formatNumberToE164(number, countryIso);
    }

    private static File getFilePath(Context context, String normalizedNumber) {
        File dir = new File(context.getCacheDir()
                + File.separator + "lookup");

        if (!dir.exists()) {
            dir.mkdirs();
        }

        return new File(dir, normalizedNumber + ".json");
    }

    public static File getImagePath(Context context, String normalizedNumber) {
        File dir = new File(context.getCacheDir()
                + File.separator + "lookup");

        if (!dir.exists()) {
            dir.mkdirs();
        }

        return new File(dir, normalizedNumber + ".webp");
    }
}