1 /*
2  * Copyright (C) 2021 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.util.Log;
19 
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22 import androidx.lifecycle.LiveData;
23 import androidx.lifecycle.MutableLiveData;
24 import androidx.lifecycle.ViewModel;
25 import androidx.lifecycle.ViewModelProvider;
26 
27 import com.android.wallpaper.module.WallpaperPersister.Destination;
28 import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback;
29 
30 /**
31  * {@link ViewModel} class that keeps track of the status of the "Set wallpaper" operation.
32  */
33 public class SetWallpaperViewModel extends ViewModel {
34 
35     private static final String TAG = "SetWallpaperViewModel";
36 
37     public enum SetWallpaperStatus {
38         UNKNOWN, PENDING, SUCCESS, ERROR
39     }
40 
41     /**
42      * @return a {@link SetWallpaperCallback} to set as listener for
43      * {@link WallpaperSetter#setCurrentWallpaper} that will update the
44      * {@link SetWallpaperViewModel} obtained from the given provider.
45      */
getCallback(@onNull ViewModelProvider provider)46     public static SetWallpaperCallback getCallback(@NonNull ViewModelProvider provider) {
47         SetWallpaperViewModel viewModel = provider.get(SetWallpaperViewModel.class);
48         return new SetWallpaperCallback() {
49             @Override
50             public void onSuccess(WallpaperInfo wallpaperInfo, @Destination int destination) {
51                 Log.d(TAG, "SetWallpaperCallback success");
52                 viewModel.mStatus.setValue(SetWallpaperStatus.SUCCESS);
53             }
54 
55             @Override
56             public void onError(@Nullable Throwable throwable) {
57                 Log.w(TAG, "SetWallpaperCallback error", throwable);
58                 viewModel.mStatus.setValue(SetWallpaperStatus.ERROR);
59             }
60         };
61     }
62 
63     private final MutableLiveData<SetWallpaperStatus> mStatus = new MutableLiveData<>();
64 
65     @Destination
66     private int mDestination;
67 
68     public SetWallpaperViewModel() {
69         mStatus.setValue(SetWallpaperStatus.UNKNOWN);
70     }
71 
72     public LiveData<SetWallpaperStatus> getStatus() {
73         return mStatus;
74     }
75 
76     public int getDestination() {
77         return mDestination;
78     }
79 
80     public void setDestination(int destination) {
81         mDestination = destination;
82     }
83 }
84