summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/providers/Folder.java
blob: c1ddf0080be0616d3365cdf9c6de78a289abefa7 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*******************************************************************************
 *      Copyright (C) 2012 Google Inc.
 *      Licensed to The Android Open Source Project.
 *
 *      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.mail.providers;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.PaintDrawable;
import android.net.Uri;
import android.net.Uri.Builder;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.android.mail.content.CursorCreator;
import com.android.mail.content.ObjectCursorLoader;
import com.android.mail.providers.UIProvider.FolderType;
import com.android.mail.utils.FolderUri;
import com.android.mail.utils.LogTag;
import com.android.mail.utils.LogUtils;
import com.android.mail.utils.Utils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

/**
 * A folder is a collection of conversations, and perhaps other folders.
 */
// TODO: make most of these fields final
public class Folder implements Parcelable, Comparable<Folder> {

    @Deprecated
    public static final String SPLITTER = "^*^";
    @Deprecated
    private static final Pattern SPLITTER_REGEX = Pattern.compile("\\^\\*\\^");

    /**
     *
     */
    private static final String FOLDER_UNINITIALIZED = "Uninitialized!";

    // TODO: remove this once we figure out which folder is returning a "null" string as the
    // conversation list uri
    private static final String NULL_STRING_URI = "null";
    private static final String LOG_TAG = LogTag.getLogTag();

    // Try to match the order of members with the order of constants in UIProvider.

    /**
     * Unique id of this folder.
     */
    public int id;

    /**
     * Persistent (across installations) id of this folder.
     */
    public String persistentId;

    /**
     * The content provider URI that returns this folder for this account.
     */
    public FolderUri folderUri;

    /**
     * The human visible name for this folder.
     */
    public String name;

    /**
     * The possible capabilities that this folder supports.
     */
    public int capabilities;

    /**
     * Whether or not this folder has children folders.
     */
    public boolean hasChildren;

    /**
     * How large the synchronization window is: how many days worth of data is retained on the
     * device.
     */
    public int syncWindow;

    /**
     * The content provider URI to return the list of conversations in this
     * folder.
     */
    public Uri conversationListUri;

    /**
     * The content provider URI to return the list of child folders of this folder.
     */
    public Uri childFoldersListUri;

    /**
     * The number of messages that are unseen in this folder.
     */
    public int unseenCount;

    /**
     * The number of messages that are unread in this folder.
     */
    public int unreadCount;

    /**
     * The total number of messages in this folder.
     */
    public int totalCount;

    /**
     * The content provider URI to force a refresh of this folder.
     */
    public Uri refreshUri;

    /**
     * The current sync status of the folder
     */
    public int syncStatus;

    /**
     * A packed integer containing the last synced result, and the request code. The
     * value is (requestCode << 4) | syncResult
     * syncResult is a value from {@link UIProvider.LastSyncResult}
     * requestCode is a value from: {@link UIProvider.SyncStatus},
     */
    public int lastSyncResult;

    /**
     * Folder type bit mask. 0 is default.
     * @see FolderType
     */
    public int type;

    /**
     * Icon for this folder; 0 implies no icon.
     */
    public int iconResId;

    /**
     * Notification icon for this folder; 0 implies no icon.
     */
    public int notificationIconResId;

    public String bgColor;
    public String fgColor;

    public int bgColorInt;
    public int fgColorInt;

    /**
     * The content provider URI to request additional conversations
     */
    public Uri loadMoreUri;

    /**
     * The possibly empty name of this folder with full hierarchy.
     * The expected format is: parent/folder1/folder2/folder3/folder4
     */
    public String hierarchicalDesc;

    /**
     * Parent folder of this folder, or null if there is none.
     */
    public Uri parent;

    /**
     * The time at which the last message was received.
     */
    public long lastMessageTimestamp;

    /** An immutable, empty conversation list */
    public static final Collection<Folder> EMPTY = Collections.emptyList();

    // TODO: we desperately need a Builder here
    public Folder(int id, String persistentId, Uri uri, String name, int capabilities,
            boolean hasChildren, int syncWindow, Uri conversationListUri, Uri childFoldersListUri,
            int unseenCount, int unreadCount, int totalCount, Uri refreshUri, int syncStatus,
            int lastSyncResult, int type, int iconResId, int notificationIconResId, String bgColor,
            String fgColor, Uri loadMoreUri, String hierarchicalDesc, Uri parent,
            final long lastMessageTimestamp) {
        this.id = id;
        this.persistentId = persistentId;
        this.folderUri = new FolderUri(uri);
        this.name = name;
        this.capabilities = capabilities;
        this.hasChildren = hasChildren;
        this.syncWindow = syncWindow;
        this.conversationListUri = conversationListUri;
        this.childFoldersListUri = childFoldersListUri;
        this.unseenCount = unseenCount;
        this.unreadCount = unreadCount;
        this.totalCount = totalCount;
        this.refreshUri = refreshUri;
        this.syncStatus = syncStatus;
        this.lastSyncResult = lastSyncResult;
        this.type = type;
        this.iconResId = iconResId;
        this.notificationIconResId = notificationIconResId;
        this.bgColor = bgColor;
        this.fgColor = fgColor;
        if (bgColor != null) {
            this.bgColorInt = Integer.parseInt(bgColor);
        }
        if (fgColor != null) {
            this.fgColorInt = Integer.parseInt(fgColor);
        }
        this.loadMoreUri = loadMoreUri;
        this.hierarchicalDesc = hierarchicalDesc;
        this.lastMessageTimestamp = lastMessageTimestamp;
        this.parent = parent;
    }

    public Folder(Cursor cursor) {
        id = cursor.getInt(UIProvider.FOLDER_ID_COLUMN);
        persistentId = cursor.getString(UIProvider.FOLDER_PERSISTENT_ID_COLUMN);
        folderUri =
                new FolderUri(Uri.parse(cursor.getString(UIProvider.FOLDER_URI_COLUMN)));
        name = cursor.getString(UIProvider.FOLDER_NAME_COLUMN);
        capabilities = cursor.getInt(UIProvider.FOLDER_CAPABILITIES_COLUMN);
        // 1 for true, 0 for false.
        hasChildren = cursor.getInt(UIProvider.FOLDER_HAS_CHILDREN_COLUMN) == 1;
        syncWindow = cursor.getInt(UIProvider.FOLDER_SYNC_WINDOW_COLUMN);
        String convList = cursor.getString(UIProvider.FOLDER_CONVERSATION_LIST_URI_COLUMN);
        conversationListUri = !TextUtils.isEmpty(convList) ? Uri.parse(convList) : null;
        String childList = cursor.getString(UIProvider.FOLDER_CHILD_FOLDERS_LIST_COLUMN);
        childFoldersListUri = (hasChildren && !TextUtils.isEmpty(childList)) ? Uri.parse(childList)
                : null;
        unseenCount = cursor.getInt(UIProvider.FOLDER_UNSEEN_COUNT_COLUMN);
        unreadCount = cursor.getInt(UIProvider.FOLDER_UNREAD_COUNT_COLUMN);
        totalCount = cursor.getInt(UIProvider.FOLDER_TOTAL_COUNT_COLUMN);
        String refresh = cursor.getString(UIProvider.FOLDER_REFRESH_URI_COLUMN);
        refreshUri = !TextUtils.isEmpty(refresh) ? Uri.parse(refresh) : null;
        syncStatus = cursor.getInt(UIProvider.FOLDER_SYNC_STATUS_COLUMN);
        lastSyncResult = cursor.getInt(UIProvider.FOLDER_LAST_SYNC_RESULT_COLUMN);
        type = cursor.getInt(UIProvider.FOLDER_TYPE_COLUMN);
        iconResId = cursor.getInt(UIProvider.FOLDER_ICON_RES_ID_COLUMN);
        notificationIconResId = cursor.getInt(UIProvider.FOLDER_NOTIFICATION_ICON_RES_ID_COLUMN);
        bgColor = cursor.getString(UIProvider.FOLDER_BG_COLOR_COLUMN);
        fgColor = cursor.getString(UIProvider.FOLDER_FG_COLOR_COLUMN);
        if (bgColor != null) {
            bgColorInt = Integer.parseInt(bgColor);
        }
        if (fgColor != null) {
            fgColorInt = Integer.parseInt(fgColor);
        }
        String loadMore = cursor.getString(UIProvider.FOLDER_LOAD_MORE_URI_COLUMN);
        loadMoreUri = !TextUtils.isEmpty(loadMore) ? Uri.parse(loadMore) : null;
        hierarchicalDesc = cursor.getString(UIProvider.FOLDER_HIERARCHICAL_DESC_COLUMN);
        lastMessageTimestamp = cursor.getLong(UIProvider.FOLDER_LAST_MESSAGE_TIMESTAMP_COLUMN);
        // A null parent URI means that this is a top-level folder.
        final String parentString = cursor.getString(UIProvider.FOLDER_PARENT_URI_COLUMN);
        parent = parentString == null ? Uri.EMPTY : Uri.parse(parentString);
    }

    /**
     * Public object that knows how to construct Folders given Cursors.
     */
    public static final CursorCreator<Folder> FACTORY = new CursorCreator<Folder>() {
        @Override
        public Folder createFromCursor(Cursor c) {
            return new Folder(c);
        }

        @Override
        public String toString() {
            return "Folder CursorCreator";
        }
    };

    public Folder(Parcel in, ClassLoader loader) {
        id = in.readInt();
        persistentId = in.readString();
        folderUri = new FolderUri((Uri) in.readParcelable(loader));
        name = in.readString();
        capabilities = in.readInt();
        // 1 for true, 0 for false.
        hasChildren = in.readInt() == 1;
        syncWindow = in.readInt();
        conversationListUri = in.readParcelable(loader);
        childFoldersListUri = in.readParcelable(loader);
        unseenCount = in.readInt();
        unreadCount = in.readInt();
        totalCount = in.readInt();
        refreshUri = in.readParcelable(loader);
        syncStatus = in.readInt();
        lastSyncResult = in.readInt();
        type = in.readInt();
        iconResId = in.readInt();
        notificationIconResId = in.readInt();
        bgColor = in.readString();
        fgColor = in.readString();
        if (bgColor != null) {
            bgColorInt = Integer.parseInt(bgColor);
        }
        if (fgColor != null) {
            fgColorInt = Integer.parseInt(fgColor);
        }
        loadMoreUri = in.readParcelable(loader);
        hierarchicalDesc = in.readString();
        parent = in.readParcelable(loader);
        lastMessageTimestamp = in.readLong();
        parent = in.readParcelable(loader);
     }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(persistentId);
        dest.writeParcelable(folderUri != null ? folderUri.fullUri : null, 0);
        dest.writeString(name);
        dest.writeInt(capabilities);
        // 1 for true, 0 for false.
        dest.writeInt(hasChildren ? 1 : 0);
        dest.writeInt(syncWindow);
        dest.writeParcelable(conversationListUri, 0);
        dest.writeParcelable(childFoldersListUri, 0);
        dest.writeInt(unseenCount);
        dest.writeInt(unreadCount);
        dest.writeInt(totalCount);
        dest.writeParcelable(refreshUri, 0);
        dest.writeInt(syncStatus);
        dest.writeInt(lastSyncResult);
        dest.writeInt(type);
        dest.writeInt(iconResId);
        dest.writeInt(notificationIconResId);
        dest.writeString(bgColor);
        dest.writeString(fgColor);
        dest.writeParcelable(loadMoreUri, 0);
        dest.writeString(hierarchicalDesc);
        dest.writeParcelable(parent, 0);
        dest.writeLong(lastMessageTimestamp);
        dest.writeParcelable(parent, 0);
    }

    /**
     * Construct a folder that queries for search results. Do not call on the UI
     * thread.
     */
    public static ObjectCursorLoader<Folder> forSearchResults(Account account, String query,
            Context context) {
        if (account.searchUri != null) {
            final Builder searchBuilder = account.searchUri.buildUpon();
            searchBuilder.appendQueryParameter(UIProvider.SearchQueryParameters.QUERY, query);
            final Uri searchUri = searchBuilder.build();
            return new ObjectCursorLoader<Folder>(context, searchUri, UIProvider.FOLDERS_PROJECTION,
                    FACTORY);
        }
        return null;
    }

    public static HashMap<Uri, Folder> hashMapForFolders(List<Folder> rawFolders) {
        final HashMap<Uri, Folder> folders = new HashMap<Uri, Folder>();
        for (Folder f : rawFolders) {
            folders.put(f.folderUri.getComparisonUri(), f);
        }
        return folders;
    }

    /**
     * Constructor that leaves everything uninitialized.
     */
    private Folder() {
        name = FOLDER_UNINITIALIZED;
    }

    /**
     * Creates a new instance of a folder object that is <b>not</b> initialized.  The caller is
     * expected to fill in the details. Used only for testing.
     * @return a new instance of an unsafe folder.
     */
    @VisibleForTesting
    public static Folder newUnsafeInstance() {
        return new Folder();
    }

    public static final ClassLoaderCreator<Folder> CREATOR = new ClassLoaderCreator<Folder>() {
        @Override
        public Folder createFromParcel(Parcel source) {
            return new Folder(source, null);
        }

        @Override
        public Folder createFromParcel(Parcel source, ClassLoader loader) {
            return new Folder(source, loader);
        }

        @Override
        public Folder[] newArray(int size) {
            return new Folder[size];
        }
    };

    @Override
    public int describeContents() {
        // Return a sort of version number for this parcelable folder. Starting with zero.
        return 0;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || !(o instanceof Folder)) {
            return false;
        }
        return Objects.equal(folderUri, ((Folder) o).folderUri);
    }

    @Override
    public int hashCode() {
        return folderUri == null ? 0 : folderUri.hashCode();
    }

    @Override
    public String toString() {
        // log extra info at DEBUG level or finer
        final StringBuilder sb = new StringBuilder("[folder id=");
        sb.append(id);
        if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)) {
            sb.append(", uri=");
            sb.append(folderUri);
            sb.append(", name=");
            sb.append(name);
        }
        sb.append("]");
        return sb.toString();
    }

    @Override
    public int compareTo(Folder other) {
        return name.compareToIgnoreCase(other.name);
    }

    /**
     * Returns a boolean indicating whether network activity (sync) is occuring for this folder.
     */
    public boolean isSyncInProgress() {
        return UIProvider.SyncStatus.isSyncInProgress(syncStatus);
    }

    public boolean supportsCapability(int capability) {
        return (capabilities & capability) != 0;
    }

    // Show black text on a transparent swatch for system folders, effectively hiding the
    // swatch (see bug 2431925).
    public static void setFolderBlockColor(Folder folder, View colorBlock) {
        if (colorBlock == null) {
            return;
        }
        boolean showBg =
                !TextUtils.isEmpty(folder.bgColor) && (folder.type & FolderType.INBOX_SECTION) == 0;
        final int backgroundColor = showBg ? Integer.parseInt(folder.bgColor) : 0;
        if (backgroundColor == Utils.getDefaultFolderBackgroundColor(colorBlock.getContext())) {
            showBg = false;
        }
        if (!showBg) {
            colorBlock.setBackgroundDrawable(null);
            colorBlock.setVisibility(View.GONE);
        } else {
            PaintDrawable paintDrawable = new PaintDrawable();
            paintDrawable.getPaint().setColor(backgroundColor);
            colorBlock.setBackgroundDrawable(paintDrawable);
            colorBlock.setVisibility(View.VISIBLE);
        }
    }

    public static void setIcon(Folder folder, ImageView iconView) {
        if (iconView == null) {
            return;
        }
        final int icon = folder.iconResId;
        if (icon > 0) {
            iconView.setImageResource(icon);
            iconView.setVisibility(View.VISIBLE);
        } else {
            iconView.setVisibility(View.GONE);
        }
    }

    /**
     * Return if the type of the folder matches a provider defined folder.
     */
    public boolean isProviderFolder() {
        return !isType(UIProvider.FolderType.DEFAULT);
    }

    public int getBackgroundColor(int defaultColor) {
        return bgColor != null ? bgColorInt : defaultColor;
    }

    public int getForegroundColor(int defaultColor) {
        return fgColor != null ? fgColorInt : defaultColor;
    }

    /**
     * Get just the uri's from an arraylist of folders.
     */
    public static String[] getUriArray(List<Folder> folders) {
        if (folders == null || folders.size() == 0) {
            return new String[0];
        }
        final String[] folderUris = new String[folders.size()];
        int i = 0;
        for (Folder folder : folders) {
            folderUris[i] = folder.folderUri.toString();
            i++;
        }
        return folderUris;
    }

    /**
     * Returns a boolean indicating whether this Folder object has been initialized
     */
    public boolean isInitialized() {
        return !name.equals(FOLDER_UNINITIALIZED) && conversationListUri != null &&
                !NULL_STRING_URI.equals(conversationListUri.toString());
    }

    public boolean isType(final int folderType) {
        return isType(type, folderType);
    }

    /**
     * Checks if <code>typeMask</code> is of the specified {@link FolderType}
     *
     * @return <code>true</code> if the mask contains the specified
     *         {@link FolderType}, <code>false</code> otherwise
     */
    public static boolean isType(final int typeMask, final int folderType) {
        return (typeMask & folderType) != 0;
    }

    public boolean isInbox() {
        return isType(UIProvider.FolderType.INBOX);
    }

    /**
     * Return if this is the trash folder.
     */
    public boolean isTrash() {
        return isType(UIProvider.FolderType.TRASH);
    }

    /**
     * Return if this is a draft folder.
     */
    public boolean isDraft() {
        return isType(UIProvider.FolderType.DRAFT);
    }

    /**
     * Whether this folder supports only showing important messages.
     */
    public boolean isImportantOnly() {
        return supportsCapability(
                UIProvider.FolderCapabilities.ONLY_IMPORTANT);
    }

    /**
     * Whether this is the special folder just used to display all mail for an account.
     */
    public boolean isViewAll() {
        return isType(UIProvider.FolderType.ALL_MAIL);
    }

    /**
     * True if the previous sync was successful, false otherwise.
     * @return
     */
    public final boolean wasSyncSuccessful() {
        return ((lastSyncResult & 0x0f) == UIProvider.LastSyncResult.SUCCESS);
    }

    /**
     * Returns true if unread count should be suppressed for this folder. This is done for folders
     * where the unread count is meaningless: trash or drafts, for instance.
     * @return true if unread count should be suppressed for this object.
     */
    public final boolean isUnreadCountHidden() {
        return (isDraft() || isTrash() || isType(FolderType.OUTBOX));
    }

    @Deprecated
    public static Folder fromString(String inString) {
         if (TextUtils.isEmpty(inString)) {
             return null;
         }
         final Folder f = new Folder();
         int indexOf = inString.indexOf(SPLITTER);
         int id = -1;
         if (indexOf != -1) {
             id = Integer.valueOf(inString.substring(0, indexOf));
         } else {
             // If no separator was found, we can't parse this folder and the
             // TextUtils.split call would also fail. Return null.
             return null;
         }
         final String[] split = TextUtils.split(inString, SPLITTER_REGEX);
         if (split.length < 20) {
             LogUtils.e(LOG_TAG, "split.length %d", split.length);
             return null;
         }
         f.id = id;
         int index = 1;
         f.folderUri = new FolderUri(Folder.getValidUri(split[index++]));
         f.name = split[index++];
         f.hasChildren = Integer.parseInt(split[index++]) != 0;
         f.capabilities = Integer.parseInt(split[index++]);
         f.syncWindow = Integer.parseInt(split[index++]);
         f.conversationListUri = getValidUri(split[index++]);
         f.childFoldersListUri = getValidUri(split[index++]);
         f.unreadCount = Integer.parseInt(split[index++]);
         f.totalCount = Integer.parseInt(split[index++]);
         f.refreshUri = getValidUri(split[index++]);
         f.syncStatus = Integer.parseInt(split[index++]);
         f.lastSyncResult = Integer.parseInt(split[index++]);
         f.type = Integer.parseInt(split[index++]);
         f.iconResId = Integer.parseInt(split[index++]);
         f.bgColor = split[index++];
         f.fgColor = split[index++];
         if (f.bgColor != null) {
             f.bgColorInt = Integer.parseInt(f.bgColor);
         }
         if (f.fgColor != null) {
             f.fgColorInt = Integer.parseInt(f.fgColor);
         }
         f.loadMoreUri = getValidUri(split[index++]);
         f.hierarchicalDesc = split[index++];
         f.parent = Folder.getValidUri(split[index++]);
         return f;
     }

    private static Uri getValidUri(String uri) {
         if (TextUtils.isEmpty(uri)) {
             return null;
         }
         return Uri.parse(uri);
    }
}