Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.wallpaper.model; |
| 17 | |
| 18 | import android.app.Activity; |
| 19 | import android.content.Context; |
| 20 | import android.os.Parcel; |
| 21 | import android.os.ParcelFileDescriptor; |
| 22 | import android.util.Log; |
| 23 | |
Santiago Etchebehere | e6d69b5 | 2019-06-14 18:42:46 -0700 | [diff] [blame] | 24 | import androidx.annotation.DrawableRes; |
| 25 | import androidx.annotation.StringRes; |
| 26 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 27 | import com.android.wallpaper.asset.Asset; |
| 28 | import com.android.wallpaper.asset.BuiltInWallpaperAsset; |
| 29 | import com.android.wallpaper.asset.CurrentWallpaperAssetVN; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 30 | import com.android.wallpaper.compat.WallpaperManagerCompat; |
| 31 | import com.android.wallpaper.compat.WallpaperManagerCompat.WallpaperLocation; |
| 32 | import com.android.wallpaper.module.InjectorProvider; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 33 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 34 | import java.io.IOException; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | |
| 38 | /** |
| 39 | * Represents the currently set wallpaper on N+ devices. Should not be used to set a new wallpaper. |
| 40 | */ |
| 41 | public class CurrentWallpaperInfoVN extends WallpaperInfo { |
| 42 | |
| 43 | public static final Creator<CurrentWallpaperInfoVN> CREATOR = |
| 44 | new Creator<CurrentWallpaperInfoVN>() { |
| 45 | @Override |
| 46 | public CurrentWallpaperInfoVN createFromParcel(Parcel source) { |
| 47 | return new CurrentWallpaperInfoVN(source); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public CurrentWallpaperInfoVN[] newArray(int size) { |
| 52 | return new CurrentWallpaperInfoVN[size]; |
| 53 | } |
| 54 | }; |
| 55 | private static final String TAG = "CurrentWallpaperInfoVN"; |
| 56 | private List<String> mAttributions; |
| 57 | private Asset mAsset; |
| 58 | private String mActionUrl; |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 59 | @StringRes |
| 60 | private int mActionLabelRes; |
| 61 | @DrawableRes |
| 62 | private int mActionIconRes; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 63 | private String mCollectionId; |
| 64 | @WallpaperLocation |
| 65 | private int mWallpaperManagerFlag; |
| 66 | |
| 67 | /** |
| 68 | * Constructs a new instance of this class. |
| 69 | * |
| 70 | * @param wallpaperManagerFlag Either SYSTEM or LOCK--the source of image data which this object |
| 71 | * represents. |
| 72 | */ |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 73 | public CurrentWallpaperInfoVN(List<String> attributions, String actionUrl, |
| 74 | @StringRes int actionLabelRes, @DrawableRes int actionIconRes, |
| 75 | String collectionId, |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 76 | @WallpaperLocation int wallpaperManagerFlag) { |
| 77 | mAttributions = attributions; |
| 78 | mWallpaperManagerFlag = wallpaperManagerFlag; |
| 79 | mActionUrl = actionUrl; |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 80 | mActionLabelRes = actionLabelRes; |
| 81 | mActionIconRes = actionIconRes; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 82 | mCollectionId = collectionId; |
| 83 | } |
| 84 | |
| 85 | private CurrentWallpaperInfoVN(Parcel in) { |
| 86 | mAttributions = new ArrayList<>(); |
| 87 | in.readStringList(mAttributions); |
| 88 | //noinspection ResourceType |
| 89 | mWallpaperManagerFlag = in.readInt(); |
| 90 | mActionUrl = in.readString(); |
| 91 | mCollectionId = in.readString(); |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 92 | mActionLabelRes = in.readInt(); |
| 93 | mActionIconRes = in.readInt(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public List<String> getAttributions(Context context) { |
| 98 | return mAttributions; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Asset getAsset(Context context) { |
| 103 | if (mAsset == null) { |
| 104 | mAsset = createCurrentWallpaperAssetVN(context); |
| 105 | } |
| 106 | return mAsset; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Asset getThumbAsset(Context context) { |
| 111 | return getAsset(context); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public String getActionUrl(Context unused) { |
| 116 | return mActionUrl; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public String getCollectionId(Context unused) { |
| 121 | return mCollectionId; |
| 122 | } |
| 123 | |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 124 | @Override |
Santiago Etchebehere | e0810d0 | 2018-05-10 17:39:40 -0700 | [diff] [blame] | 125 | public int getActionIconRes(Context unused) { |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 126 | return mActionIconRes != 0 ? mActionIconRes : WallpaperInfo.getDefaultActionIcon(); |
| 127 | } |
| 128 | |
| 129 | @Override |
Santiago Etchebehere | e0810d0 | 2018-05-10 17:39:40 -0700 | [diff] [blame] | 130 | public int getActionLabelRes(Context unused) { |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 131 | return mActionLabelRes != 0 ? mActionLabelRes : WallpaperInfo.getDefaultActionLabel(); |
| 132 | } |
| 133 | |
Santiago Etchebehere | e6d69b5 | 2019-06-14 18:42:46 -0700 | [diff] [blame] | 134 | public int getWallpaperManagerFlag() { |
| 135 | return mWallpaperManagerFlag; |
| 136 | } |
| 137 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 138 | /** |
| 139 | * Constructs and returns an Asset instance representing the currently-set wallpaper asset. |
| 140 | */ |
| 141 | private Asset createCurrentWallpaperAssetVN(Context context) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 142 | WallpaperManagerCompat wallpaperManagerCompat = InjectorProvider.getInjector() |
| 143 | .getWallpaperManagerCompat(context); |
| 144 | |
| 145 | ParcelFileDescriptor systemWallpaperFile = wallpaperManagerCompat.getWallpaperFile( |
| 146 | WallpaperManagerCompat.FLAG_SYSTEM); |
| 147 | |
| 148 | // Whether the wallpaper this object represents is the default built-in wallpaper. |
| 149 | boolean isSystemBuiltIn = mWallpaperManagerFlag == WallpaperManagerCompat.FLAG_SYSTEM |
| 150 | && systemWallpaperFile == null; |
| 151 | |
| 152 | if (systemWallpaperFile != null) { |
| 153 | try { |
| 154 | systemWallpaperFile.close(); |
| 155 | } catch (IOException e) { |
| 156 | Log.e(TAG, "Unable to close system wallpaper ParcelFileDescriptor", e); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return (isSystemBuiltIn) |
| 161 | ? new BuiltInWallpaperAsset(context) |
| 162 | : new CurrentWallpaperAssetVN(context, mWallpaperManagerFlag); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void writeToParcel(Parcel parcel, int flags) { |
| 167 | parcel.writeStringList(mAttributions); |
| 168 | parcel.writeInt(mWallpaperManagerFlag); |
| 169 | parcel.writeString(mActionUrl); |
| 170 | parcel.writeString(mCollectionId); |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 171 | parcel.writeInt(mActionLabelRes); |
| 172 | parcel.writeInt(mActionIconRes); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, |
| 177 | int requestCode) { |
| 178 | srcActivity.startActivityForResult(factory.newIntent(srcActivity, this), requestCode); |
| 179 | } |
| 180 | } |