Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 1 | package 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 | |
| 19 | import android.content.ContentValues; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.graphics.Bitmap; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 23 | import android.net.Uri; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 24 | import android.os.UserHandle; |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 25 | |
| 26 | import com.android.launcher3.LauncherAppState; |
| 27 | import com.android.launcher3.LauncherSettings; |
| 28 | import com.android.launcher3.Utilities; |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 29 | import com.android.launcher3.compat.UserManagerCompat; |
| 30 | |
| 31 | /** |
| 32 | * A wrapper around {@link ContentValues} with some utility methods. |
| 33 | */ |
| 34 | public class ContentWriter { |
| 35 | |
| 36 | private final ContentValues mValues; |
| 37 | private final Context mContext; |
| 38 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 39 | private CommitParams mCommitParams; |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 40 | private Bitmap mIcon; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 41 | private UserHandle mUser; |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 42 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 43 | public ContentWriter(Context context, CommitParams commitParams) { |
| 44 | this(context); |
| 45 | mCommitParams = commitParams; |
| 46 | } |
| 47 | |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 48 | public ContentWriter(Context context) { |
| 49 | this(new ContentValues(), context); |
| 50 | } |
| 51 | |
| 52 | public ContentWriter(ContentValues values, Context context) { |
| 53 | mValues = values; |
| 54 | mContext = context; |
| 55 | } |
| 56 | |
| 57 | public ContentWriter put(String key, Integer value) { |
| 58 | mValues.put(key, value); |
| 59 | return this; |
| 60 | } |
| 61 | |
| 62 | public ContentWriter put(String key, Long value) { |
| 63 | mValues.put(key, value); |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | public ContentWriter put(String key, String value) { |
| 68 | mValues.put(key, value); |
| 69 | return this; |
| 70 | } |
| 71 | |
| 72 | public ContentWriter put(String key, CharSequence value) { |
| 73 | mValues.put(key, value == null ? null : value.toString()); |
| 74 | return this; |
| 75 | } |
| 76 | |
| 77 | public ContentWriter put(String key, Intent value) { |
| 78 | mValues.put(key, value == null ? null : value.toUri(0)); |
| 79 | return this; |
| 80 | } |
| 81 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 82 | public ContentWriter putIcon(Bitmap value, UserHandle user) { |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 83 | mIcon = value; |
| 84 | mUser = user; |
| 85 | return this; |
| 86 | } |
| 87 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 88 | public ContentWriter put(String key, UserHandle user) { |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 89 | return put(key, UserManagerCompat.getInstance(mContext).getSerialNumberForUser(user)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Commits any pending validation and returns the final values. |
| 94 | * Must not be called on UI thread. |
| 95 | */ |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame^] | 96 | public ContentValues getValues(Context context) { |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 97 | Preconditions.assertNonUiThread(); |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame^] | 98 | if (mIcon != null && !LauncherAppState.getInstance(context).getIconCache() |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 99 | .isDefaultIcon(mIcon, mUser)) { |
| 100 | mValues.put(LauncherSettings.Favorites.ICON, Utilities.flattenBitmap(mIcon)); |
| 101 | mIcon = null; |
| 102 | } |
| 103 | return mValues; |
| 104 | } |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 105 | |
| 106 | public int commit() { |
| 107 | if (mCommitParams != null) { |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame^] | 108 | return mContext.getContentResolver().update(mCommitParams.mUri, getValues(mContext), |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 109 | mCommitParams.mWhere, mCommitParams.mSelectionArgs); |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | public static final class CommitParams { |
| 115 | |
| 116 | final Uri mUri = LauncherSettings.Favorites.CONTENT_URI; |
| 117 | String mWhere; |
| 118 | String[] mSelectionArgs; |
| 119 | |
| 120 | public CommitParams(String where, String[] selectionArgs) { |
| 121 | mWhere = where; |
| 122 | mSelectionArgs = selectionArgs; |
| 123 | } |
| 124 | |
| 125 | } |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 126 | } |