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