summaryrefslogtreecommitdiff
path: root/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java
blob: 3ba64ecc361b4f287306899beae508b440c1015a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.customization.picker.theme;

import static android.app.Activity.RESULT_OK;

import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY;
import static com.android.wallpaper.widget.BottomActionBar.BottomAction.INFORMATION;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.customization.model.theme.DefaultThemeProvider;
import com.android.customization.model.theme.ThemeBundle;
import com.android.customization.model.theme.ThemeBundleProvider;
import com.android.customization.module.CustomizationInjector;
import com.android.customization.picker.WallpaperPreviewer;
import com.android.wallpaper.R;
import com.android.wallpaper.model.WallpaperInfo;
import com.android.wallpaper.module.InjectorProvider;
import com.android.wallpaper.picker.AppbarFragment;
import com.android.wallpaper.widget.BottomActionBar;
import com.android.wallpaper.widget.BottomActionBar.BottomSheetContent;

import com.bumptech.glide.Glide;

import org.json.JSONException;

/** A Fragment for theme full preview page. */
public class ThemeFullPreviewFragment extends AppbarFragment {
    private static final String TAG = "ThemeFullPreviewFragment";

    public static final String EXTRA_THEME_OPTION_TITLE = "theme_option_title";
    protected static final String EXTRA_THEME_OPTION = "theme_option";
    protected static final String EXTRA_WALLPAPER_INFO = "wallpaper_info";
    protected static final String EXTRA_CAN_APPLY_FROM_FULL_PREVIEW = "can_apply";

    private WallpaperInfo mWallpaper;
    private ThemeBundle mThemeBundle;
    private boolean mCanApplyFromFullPreview;

    /**
     * Returns a new {@link ThemeFullPreviewFragment} with the provided title and bundle arguments
     * set.
     */
    public static ThemeFullPreviewFragment newInstance(CharSequence title, Bundle intentBundle) {
        ThemeFullPreviewFragment fragment = new ThemeFullPreviewFragment();
        Bundle bundle = new Bundle();
        bundle.putAll(AppbarFragment.createArguments(title));
        bundle.putAll(intentBundle);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWallpaper = getArguments().getParcelable(EXTRA_WALLPAPER_INFO);
        mCanApplyFromFullPreview = getArguments().getBoolean(EXTRA_CAN_APPLY_FROM_FULL_PREVIEW);
        CustomizationInjector injector = (CustomizationInjector) InjectorProvider.getInjector();
        ThemeBundleProvider themeProvider = new DefaultThemeProvider(
                getContext(), injector.getCustomizationPreferences(getContext()));
        try {
            ThemeBundle.Builder builder = themeProvider.parseThemeBundle(
                    getArguments().getString(EXTRA_THEME_OPTION));
            if (builder != null) {
                builder.setTitle(getArguments().getString(EXTRA_THEME_OPTION_TITLE));
                mThemeBundle = builder.build(getContext());
            }
        } catch (JSONException e) {
            Log.w(TAG, "Couldn't parse provided custom theme, will override it");
            // TODO(chihhangchuang): Handle the error case.
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(
                R.layout.fragment_theme_full_preview, container, /* attachToRoot */ false);
        setUpToolbar(view);
        Glide.get(getContext()).clearMemory();

        // Set theme option.
        final ThemeOptionPreviewer themeOptionPreviewer = new ThemeOptionPreviewer(
                getLifecycle(),
                getContext(),
                view.findViewById(R.id.theme_preview_container));
        themeOptionPreviewer.setPreviewInfo(mThemeBundle.getPreviewInfo());

        // Set wallpaper background.
        ImageView wallpaperImageView = view.findViewById(R.id.wallpaper_preview_image);
        final WallpaperPreviewer wallpaperPreviewer = new WallpaperPreviewer(
                getLifecycle(),
                getActivity(),
                wallpaperImageView,
                view.findViewById(R.id.wallpaper_preview_surface));
        wallpaperPreviewer.setWallpaper(mWallpaper,
                themeOptionPreviewer::updateColorForLauncherWidgets);
        return view;
    }

    @Override
    protected void onBottomActionBarReady(BottomActionBar bottomActionBar) {
        super.onBottomActionBarReady(bottomActionBar);
        if (mCanApplyFromFullPreview) {
            bottomActionBar.showActionsOnly(INFORMATION, APPLY);
            bottomActionBar.setActionClickListener(APPLY, v -> finishActivityWithResultOk());
        } else {
            bottomActionBar.showActionsOnly(INFORMATION);
        }
        bottomActionBar.bindBottomSheetContentWithAction(
                new ThemeInfoContent(getContext()), INFORMATION);
        bottomActionBar.show();
    }

    private void finishActivityWithResultOk() {
        Activity activity = requireActivity();
        activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        Intent intent = new Intent();
        activity.setResult(RESULT_OK, intent);
        activity.finish();
    }

    private final class ThemeInfoContent extends BottomSheetContent<ThemeInfoView> {

        private ThemeInfoContent(Context context) {
            super(context);
        }

        @Override
        public int getViewId() {
            return R.layout.theme_info_view;
        }

        @Override
        public void onViewCreated(ThemeInfoView view) {
            view.populateThemeInfo(mThemeBundle);
        }
    }
}