blob: 2d643531e8e295ef6f639de6814d242528a011d2 [file] [log] [blame]
Sunny Goyal32f3dda2016-11-11 11:45:00 -08001package com.android.launcher3.util;
2
3/**
4 * Copyright (C) 2016 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19import android.content.ContentValues;
20import android.content.Context;
21import android.content.Intent;
22import android.graphics.Bitmap;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080023import android.net.Uri;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080024import android.os.UserHandle;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080025
26import com.android.launcher3.LauncherAppState;
27import com.android.launcher3.LauncherSettings;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080028import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal3808a692019-10-25 13:41:28 -070029import com.android.launcher3.icons.BitmapInfo;
Sunny Goyale62d2bb2018-11-06 10:28:37 -080030import com.android.launcher3.icons.GraphicsUtils;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080031
32/**
33 * A wrapper around {@link ContentValues} with some utility methods.
34 */
35public class ContentWriter {
36
37 private final ContentValues mValues;
38 private final Context mContext;
39
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080040 private CommitParams mCommitParams;
Sunny Goyal3808a692019-10-25 13:41:28 -070041 private BitmapInfo mIcon;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080042 private UserHandle mUser;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080043
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080044 public ContentWriter(Context context, CommitParams commitParams) {
45 this(context);
46 mCommitParams = commitParams;
47 }
48
Sunny Goyal32f3dda2016-11-11 11:45:00 -080049 public ContentWriter(Context context) {
50 this(new ContentValues(), context);
51 }
52
53 public ContentWriter(ContentValues values, Context context) {
54 mValues = values;
55 mContext = context;
56 }
57
58 public ContentWriter put(String key, Integer value) {
59 mValues.put(key, value);
60 return this;
61 }
62
63 public ContentWriter put(String key, Long value) {
64 mValues.put(key, value);
65 return this;
66 }
67
68 public ContentWriter put(String key, String value) {
69 mValues.put(key, value);
70 return this;
71 }
72
73 public ContentWriter put(String key, CharSequence value) {
74 mValues.put(key, value == null ? null : value.toString());
75 return this;
76 }
77
78 public ContentWriter put(String key, Intent value) {
79 mValues.put(key, value == null ? null : value.toUri(0));
80 return this;
81 }
82
Sunny Goyal3808a692019-10-25 13:41:28 -070083 public ContentWriter putIcon(BitmapInfo value, UserHandle user) {
Sunny Goyal32f3dda2016-11-11 11:45:00 -080084 mIcon = value;
85 mUser = user;
86 return this;
87 }
88
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080089 public ContentWriter put(String key, UserHandle user) {
Sunny Goyal32f3dda2016-11-11 11:45:00 -080090 return put(key, UserManagerCompat.getInstance(mContext).getSerialNumberForUser(user));
91 }
92
93 /**
94 * Commits any pending validation and returns the final values.
95 * Must not be called on UI thread.
96 */
Sunny Goyal87f784c2017-01-11 10:48:34 -080097 public ContentValues getValues(Context context) {
Sunny Goyal32f3dda2016-11-11 11:45:00 -080098 Preconditions.assertNonUiThread();
Sunny Goyal87f784c2017-01-11 10:48:34 -080099 if (mIcon != null && !LauncherAppState.getInstance(context).getIconCache()
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800100 .isDefaultIcon(mIcon, mUser)) {
Sunny Goyal3808a692019-10-25 13:41:28 -0700101 mValues.put(LauncherSettings.Favorites.ICON, GraphicsUtils.flattenBitmap(mIcon.icon));
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800102 mIcon = null;
103 }
104 return mValues;
105 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800106
107 public int commit() {
108 if (mCommitParams != null) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800109 return mContext.getContentResolver().update(mCommitParams.mUri, getValues(mContext),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800110 mCommitParams.mWhere, mCommitParams.mSelectionArgs);
111 }
112 return 0;
113 }
114
115 public static final class CommitParams {
116
117 final Uri mUri = LauncherSettings.Favorites.CONTENT_URI;
118 String mWhere;
119 String[] mSelectionArgs;
120
121 public CommitParams(String where, String[] selectionArgs) {
122 mWhere = where;
123 mSelectionArgs = selectionArgs;
124 }
125
126 }
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800127}