1 /*
2  * Copyright (C) 2022 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.widget;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.widget.CompoundButton;
23 import android.widget.FrameLayout;
24 import android.widget.ToggleButton;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.appcompat.content.res.AppCompatResources;
29 
30 import com.android.wallpaper.R;
31 
32 /**
33  * Custom layout for the download button.
34  */
35 public final class WallpaperDownloadButton extends FrameLayout {
36 
37     ToggleButton mDownloadButton;
38     View mDownloadActionProgressBar;
39 
40     /**
41      * Constructor
42      */
WallpaperDownloadButton(@onNull Context context, @Nullable AttributeSet attrs)43     public WallpaperDownloadButton(@NonNull Context context, @Nullable AttributeSet attrs) {
44         super(context, attrs);
45         LayoutInflater.from(context).inflate(R.layout.button_download_wallpaper, this, true);
46         mDownloadButton = findViewById(R.id.download_button);
47         mDownloadActionProgressBar = findViewById(R.id.action_download_progress);
48     }
49 
50     /**
51      * Set {@link CompoundButton.OnCheckedChangeListener }
52      */
setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener)53     public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) {
54         mDownloadButton.setOnCheckedChangeListener(listener);
55     }
56 
57     /**
58      * Show the progress bar for the download button
59      */
showDownloadActionProgress()60     public void showDownloadActionProgress() {
61         mDownloadButton.setVisibility(GONE);
62         mDownloadActionProgressBar.setVisibility(VISIBLE);
63     }
64 
65     /**
66      * Hide the progress bar for the download button
67      */
hideDownloadActionProgress()68     public void hideDownloadActionProgress() {
69         mDownloadButton.setVisibility(VISIBLE);
70         mDownloadActionProgressBar.setVisibility(GONE);
71     }
72 
73     /**
74      * Update the color in case the context theme has changed.
75      */
updateColor()76     public void updateColor() {
77         Context context = getContext();
78         if (context == null) {
79             return;
80         }
81         mDownloadButton.setForeground(null);
82         mDownloadButton.setForeground(AppCompatResources.getDrawable(context,
83                 R.drawable.wallpaper_control_button_download));
84     }
85 }
86