1 /*
2  * Copyright (C) 2019 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.customization.picker;
17 
18 import android.content.Intent;
19 import android.os.Bundle;
20 import androidx.fragment.app.FragmentActivity;
21 import androidx.fragment.app.FragmentManager;
22 import androidx.fragment.app.FragmentTransaction;
23 import com.android.customization.model.clock.BaseClockManager;
24 import com.android.customization.model.clock.Clockface;
25 import com.android.customization.model.clock.ContentProviderClockProvider;
26 import com.android.customization.picker.clock.ClockFragment;
27 import com.android.customization.picker.clock.ClockFragment.ClockFragmentHost;
28 import com.android.wallpaper.R;
29 
30 /**
31  * Activity allowing for the clock face picker to be linked to from other setup flows.
32  *
33  * This should be used with startActivityForResult. The resulting intent contains an extra
34  * "clock_face_name" with the id of the picked clock face.
35  */
36 public class ClockFacePickerActivity extends FragmentActivity implements ClockFragmentHost {
37 
38     private static final String EXTRA_CLOCK_FACE_NAME = "clock_face_name";
39 
40     private BaseClockManager mClockManager;
41 
42     @Override
onCreate(Bundle savedInstanceState)43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         setContentView(R.layout.activity_clock_face_picker);
46 
47         // Creating a class that overrides {@link ClockManager#apply} to return the clock id to the
48         // calling activity instead of putting the value into settings.
49         //
50         mClockManager = new BaseClockManager(
51                 new ContentProviderClockProvider(ClockFacePickerActivity.this)) {
52 
53             @Override
54             protected void handleApply(Clockface option, Callback callback) {
55                 Intent result = new Intent();
56                 result.putExtra(EXTRA_CLOCK_FACE_NAME, option.getId());
57                 setResult(RESULT_OK, result);
58                 callback.onSuccess();
59                 finish();
60             }
61 
62             @Override
63             protected String lookUpCurrentClock() {
64                 return getIntent().getStringExtra(EXTRA_CLOCK_FACE_NAME);
65             }
66         };
67         if (!mClockManager.isAvailable()) {
68             finish();
69         } else {
70             final FragmentManager fm = getSupportFragmentManager();
71             final FragmentTransaction fragmentTransaction = fm.beginTransaction();
72             final ClockFragment clockFragment = ClockFragment.newInstance(
73                     getString(R.string.clock_title));
74             fragmentTransaction.replace(R.id.fragment_container, clockFragment);
75             fragmentTransaction.commitNow();
76         }
77     }
78 
79     @Override
getClockManager()80     public BaseClockManager getClockManager() {
81         return mClockManager;
82     }
83 }
84