Cherish: Add support for Lockscreen clock fonts

After r16, a commit to cleanup the KeyguardClockSwtich and unify it's structure was merged.
Also, this added a new resource into the frameworks core, which allows us to overlay the custom clock font.
So, we need to overlays now to customize this. This makes me really happy because it removes the ugly method that we were doing years ago.

[nurkeinneid]:
- adapt to our ThemeUtils
- restore original copyright header - crdroid did nothing here

Change-Id: I70590910144141a534b926f884e9f08e4e3af5bc
Signed-off-by: NurKeinNeid <mralexman3000@gmail.com>
Signed-off-by: Hưng Phan <phandinhhungvp2001@gmail.com>
This commit is contained in:
El Dainosor
2023-01-02 00:22:37 -03:00
committed by Hưng Phan
parent 9786678851
commit 52b3cff099
4 changed files with 295 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2019 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:layout_gravity="center"
android:orientation="vertical">
<FrameLayout
android:id="@+id/option_tile"
android:layout_width="match_parent"
android:layout_height="75dp"
android:gravity="center"
android:layout_gravity="center"
android:paddingHorizontal="10dp"
android:paddingVertical="10dp"
android:background="@drawable/option_border_custom">
<TextClock android:id="@+id/option_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:format12Hour="hh:mm"
android:format24Hour="kk:mm"
android:textAppearance="@style/OptionTitleTextAppearance"/>
</FrameLayout>
<TextView
android:id="@+id/option_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textAppearance="@style/OptionTitleTextAppearance"/>
</LinearLayout>

View File

@@ -150,6 +150,10 @@
<string name="theme_customization_wifi_icon_title">WiFi icon style</string> <string name="theme_customization_wifi_icon_title">WiFi icon style</string>
<string name="theme_customization_wifi_icon_summary">Set custom wifi icon style</string> <string name="theme_customization_wifi_icon_summary">Set custom wifi icon style</string>
<!-- Fonts -->
<string name="theme_customization_lock_clock_title">Lockscreen clock font</string>
<string name="theme_customization_lock_clock_summary">Set LS clock font style</string>
<!-- Custom Signal bar icons --> <!-- Custom Signal bar icons -->
<string name="custom_signal_bar_title">Signal</string> <string name="custom_signal_bar_title">Signal</string>

View File

@@ -93,6 +93,12 @@
android:summary="@string/theme_customization_ui_style_summary" android:summary="@string/theme_customization_ui_style_summary"
android:fragment="com.cherish.settings.fragments.ui.UIStyles"/> android:fragment="com.cherish.settings.fragments.ui.UIStyles"/>
<Preference
android:key="android.theme.customization.lockscreen_clock_font"
android:title="@string/theme_customization_lock_clock_title"
android:summary="@string/theme_customization_lock_clock_summary"
android:fragment="com.cherish.settings.fragments.ui.LockClockFonts"/>
<!-- QS style --> <!-- QS style -->
<com.cherish.settings.preferences.SystemSettingListPreference <com.cherish.settings.preferences.SystemSettingListPreference
android:key="qs_panel_style" android:key="qs_panel_style"

View File

@@ -0,0 +1,235 @@
/*
* Copyright (C) 2021 AospExtended ROM 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.cherish.settings.fragments.ui;
import static android.os.UserHandle.USER_SYSTEM;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.SearchIndexableResource;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.text.TextUtils;
import androidx.preference.PreferenceViewHolder;
import android.view.ViewGroup.LayoutParams;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import androidx.recyclerview.widget.RecyclerView;
import android.net.Uri;
import androidx.core.content.res.ResourcesCompat;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.Indexable;
import com.android.settings.SettingsPreferenceFragment;
import com.android.internal.util.cherish.ThemeUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.List;
import java.util.Arrays;
import org.json.JSONObject;
import org.json.JSONException;
public class LockClockFonts extends SettingsPreferenceFragment {
private RecyclerView mRecyclerView;
private ThemeUtils mThemeUtils;
private String mCategory = "android.theme.customization.lockscreen_clock_font";
private List<String> mPkgs;
private ExecutorService mExecutor = Executors.newSingleThreadExecutor();
private Handler mHandler = new Handler();
private final AtomicBoolean mApplyingOverlays = new AtomicBoolean(false);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.theme_customization_lock_clock_title);
mHandler = new Handler();
mThemeUtils = new ThemeUtils(getActivity());
mPkgs = mThemeUtils.getOverlayPackagesForCategory(mCategory, "android");
Collections.sort(mPkgs);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.item_view, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
mRecyclerView.setLayoutManager(gridLayoutManager);
Adapter mAdapter = new Adapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
return view;
}
@Override
public int getMetricsCategory() {
return MetricsEvent.CHERISH_SETTINGS;
}
@Override
public void onResume() {
super.onResume();
}
public class Adapter extends RecyclerView.Adapter<Adapter.CustomViewHolder> {
Context context;
String mSelectedPkg;
String mAppliedPkg;
public Adapter(Context context) {
this.context = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lock_clock_fonts_option, parent, false);
CustomViewHolder vh = new CustomViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(CustomViewHolder holder, final int position) {
String pkg = mPkgs.get(position);
String label = getLabel(holder.itemView.getContext(), pkg);
String currentPackageName = mThemeUtils.getOverlayInfos(mCategory).stream()
.filter(info -> info.isEnabled())
.map(info -> info.packageName)
.findFirst()
.orElse("android");
holder.title.setTextSize(24);
holder.title.setTypeface(getTypeface(holder.title.getContext(), pkg));
holder.name.setVisibility(View.GONE);
if (currentPackageName.equals(pkg)) {
mAppliedPkg = pkg;
if (mSelectedPkg == null) {
mSelectedPkg = pkg;
}
}
holder.itemView.setActivated(pkg == mSelectedPkg);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mApplyingOverlays.get()) return;
updateActivatedStatus(mSelectedPkg, false);
updateActivatedStatus(pkg, true);
mSelectedPkg = pkg;
enableOverlays(position);
}
});
}
@Override
public int getItemCount() {
return mPkgs.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView title;
public CustomViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.option_title);
name = (TextView) itemView.findViewById(R.id.option_label);
}
}
private void updateActivatedStatus(String pkg, boolean isActivated) {
int index = mPkgs.indexOf(pkg);
if (index < 0) {
return;
}
RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForAdapterPosition(index);
if (holder != null && holder.itemView != null) {
holder.itemView.setActivated(isActivated);
}
}
}
public Typeface getTypeface(Context context, String pkg) {
try {
PackageManager pm = context.getPackageManager();
Resources res = pkg.equals("android") ? Resources.getSystem()
: pm.getResourcesForApplication(pkg);
return Typeface.create(res.getString(
res.getIdentifier("config_clockFontFamily",
"string", pkg)), Typeface.NORMAL);
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
public String getLabel(Context context, String pkg) {
PackageManager pm = context.getPackageManager();
try {
return pm.getApplicationInfo(pkg, 0)
.loadLabel(pm).toString();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return pkg;
}
public void enableOverlays(int position) {
mApplyingOverlays.set(true);
mExecutor.execute(() -> {
mThemeUtils.setOverlayEnabled(mCategory, mPkgs.get(position));
mHandler.post(() -> mApplyingOverlays.set(false));
});
}
}