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.os.UserHandle; 23 24 import com.android.launcher3.LauncherAppState; 25 import com.android.launcher3.LauncherSettings; 26 import com.android.launcher3.LauncherSettings.Favorites; 27 import com.android.launcher3.icons.BitmapInfo; 28 import com.android.launcher3.icons.GraphicsUtils; 29 import com.android.launcher3.model.ModelDbController; 30 import com.android.launcher3.pm.UserCache; 31 32 /** 33 * A wrapper around {@link ContentValues} with some utility methods. 34 */ 35 public class ContentWriter { 36 37 private final ContentValues mValues; 38 private final Context mContext; 39 40 private CommitParams mCommitParams; 41 private BitmapInfo mIcon; 42 private UserHandle mUser; 43 ContentWriter(Context context, CommitParams commitParams)44 public ContentWriter(Context context, CommitParams commitParams) { 45 this(context); 46 mCommitParams = commitParams; 47 } 48 ContentWriter(Context context)49 public ContentWriter(Context context) { 50 this(new ContentValues(), context); 51 } 52 ContentWriter(ContentValues values, Context context)53 public ContentWriter(ContentValues values, Context context) { 54 mValues = values; 55 mContext = context; 56 } 57 put(String key, Integer value)58 public ContentWriter put(String key, Integer value) { 59 mValues.put(key, value); 60 return this; 61 } 62 put(String key, Long value)63 public ContentWriter put(String key, Long value) { 64 mValues.put(key, value); 65 return this; 66 } 67 put(String key, String value)68 public ContentWriter put(String key, String value) { 69 mValues.put(key, value); 70 return this; 71 } 72 put(String key, CharSequence value)73 public ContentWriter put(String key, CharSequence value) { 74 mValues.put(key, value == null ? null : value.toString()); 75 return this; 76 } 77 put(String key, Intent value)78 public ContentWriter put(String key, Intent value) { 79 mValues.put(key, value == null ? null : value.toUri(0)); 80 return this; 81 } 82 putIcon(BitmapInfo value, UserHandle user)83 public ContentWriter putIcon(BitmapInfo value, UserHandle user) { 84 mIcon = value; 85 mUser = user; 86 return this; 87 } 88 put(String key, UserHandle user)89 public ContentWriter put(String key, UserHandle user) { 90 return put(key, UserCache.INSTANCE.get(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 */ getValues(Context context)97 public ContentValues getValues(Context context) { 98 Preconditions.assertNonUiThread(); 99 if (mIcon != null && !LauncherAppState.getInstance(context).getIconCache() 100 .isDefaultIcon(mIcon, mUser)) { 101 mValues.put(LauncherSettings.Favorites.ICON, GraphicsUtils.flattenBitmap(mIcon.icon)); 102 mIcon = null; 103 } 104 return mValues; 105 } 106 commit()107 public int commit() { 108 if (mCommitParams != null) { 109 return mCommitParams.mDbController.update( 110 Favorites.TABLE_NAME, getValues(mContext), 111 mCommitParams.mWhere, mCommitParams.mSelectionArgs); 112 } 113 return 0; 114 } 115 116 public static final class CommitParams { 117 118 final ModelDbController mDbController; 119 final String mWhere; 120 final String[] mSelectionArgs; 121 CommitParams(ModelDbController controller, String where, String[] selectionArgs)122 public CommitParams(ModelDbController controller, String where, String[] selectionArgs) { 123 mDbController = controller; 124 mWhere = where; 125 mSelectionArgs = selectionArgs; 126 } 127 } 128 } 129