fix: 首次提交
This commit is contained in:
30
SettingsLib/BarChartPreference/Android.bp
Normal file
30
SettingsLib/BarChartPreference/Android.bp
Normal file
@@ -0,0 +1,30 @@
|
||||
package {
|
||||
// See: http://go/android-license-faq
|
||||
// A large-scale-change added 'default_applicable_licenses' to import
|
||||
// all of the 'license_kinds' from "frameworks_base_license"
|
||||
// to get the below license kinds:
|
||||
// SPDX-license-identifier-Apache-2.0
|
||||
default_applicable_licenses: ["frameworks_base_license"],
|
||||
}
|
||||
|
||||
android_library {
|
||||
name: "SettingsLibBarChartPreference",
|
||||
use_resource_processor: true,
|
||||
defaults: [
|
||||
"SettingsLintDefaults",
|
||||
],
|
||||
|
||||
srcs: ["src/**/*.java"],
|
||||
resource_dirs: ["res"],
|
||||
|
||||
static_libs: [
|
||||
"androidx.preference_preference",
|
||||
],
|
||||
|
||||
sdk_version: "system_current",
|
||||
min_sdk_version: "21",
|
||||
apex_available: [
|
||||
"//apex_available:platform",
|
||||
"com.android.permission",
|
||||
],
|
||||
}
|
||||
23
SettingsLib/BarChartPreference/AndroidManifest.xml
Normal file
23
SettingsLib/BarChartPreference/AndroidManifest.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2018 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.
|
||||
-->
|
||||
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.android.settingslib.widget.preference.barchart">
|
||||
|
||||
<uses-sdk android:minSdkVersion="21" />
|
||||
|
||||
</manifest>
|
||||
51
SettingsLib/BarChartPreference/build.gradle
Normal file
51
SettingsLib/BarChartPreference/build.gradle
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Include this gradle file if you are building against this as a standalone gradle library project,
|
||||
* as opposed to building it as part of the git-tree. This is typically the file you want to include
|
||||
* if you create a new project in Android Studio.
|
||||
*
|
||||
* For example, you can include the following in your settings.gradle file:
|
||||
* include ':setupcompat'
|
||||
* project(':setupcompat').projectDir = new File(PATH_TO_THIS_DIRECTORY)
|
||||
*
|
||||
* And then you can include the :setupcompat project as one of your dependencies
|
||||
* dependencies {
|
||||
* implementation project(path: ':setupcompat')
|
||||
* }
|
||||
*/
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.android.library)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
}
|
||||
|
||||
android {
|
||||
// Not specifying compileSdkVersion here so clients can specify it; must be at least Q
|
||||
namespace = "com.android.settingslib.widget.preference.barchart"
|
||||
compileSdk 34
|
||||
defaultConfig {
|
||||
minSdkVersion 31
|
||||
targetSdkVersion 34
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.flags'
|
||||
}
|
||||
}
|
||||
|
||||
sourceSets.main {
|
||||
manifest.srcFile 'AndroidManifest.xml'
|
||||
java.srcDirs = ['src']
|
||||
res.srcDirs = ['res']
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_17
|
||||
targetCompatibility JavaVersion.VERSION_17
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation files('../../libs/preference-1.3.0-alpha01.aar')
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2018 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"
|
||||
xmlns:settings="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/SettingsBarChart">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/bar_chart_title"
|
||||
style="@style/SettingsBarChartTitle"/>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/bar_views_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:gravity="center|bottom">
|
||||
|
||||
<com.android.settingslib.widget.BarView
|
||||
android:id="@+id/bar_view1"
|
||||
style="@style/SettingsBarViewStyle"
|
||||
settings:barColor="@color/settings_bar_view_1_color"/>
|
||||
<com.android.settingslib.widget.BarView
|
||||
android:id="@+id/bar_view2"
|
||||
style="@style/SettingsBarViewStyle"
|
||||
settings:barColor="@color/settings_bar_view_2_color"/>
|
||||
<com.android.settingslib.widget.BarView
|
||||
android:id="@+id/bar_view3"
|
||||
style="@style/SettingsBarViewStyle"
|
||||
settings:barColor="@color/settings_bar_view_3_color"/>
|
||||
<com.android.settingslib.widget.BarView
|
||||
android:id="@+id/bar_view4"
|
||||
style="@style/SettingsBarViewStyle"
|
||||
settings:barColor="@color/settings_bar_view_4_color"/>
|
||||
</LinearLayout>
|
||||
|
||||
<Button
|
||||
android:id="@+id/bar_chart_details"
|
||||
style="@style/SettingsBarChartDetailsButton"/>
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/empty_view"
|
||||
style="@style/SettingsBarChartEmptyText"/>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2018 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="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical">
|
||||
|
||||
<View
|
||||
android:id="@+id/bar_view"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/SettingsBarChartBar"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/icon_view"
|
||||
style="@style/SettingsBarChartBarIcon"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/bar_title"
|
||||
style="@style/SettingsBarChartBarTitle"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/bar_summary"
|
||||
style="@style/SettingsBarChartBarSummary"/>
|
||||
|
||||
</LinearLayout>
|
||||
25
SettingsLib/BarChartPreference/res/values/attrs.xml
Normal file
25
SettingsLib/BarChartPreference/res/values/attrs.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2018 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.
|
||||
-->
|
||||
|
||||
<resources>
|
||||
|
||||
<declare-styleable name="SettingsBarView">
|
||||
<!-- The color of bar view -->
|
||||
<attr name="barColor" format="color" />
|
||||
</declare-styleable>
|
||||
|
||||
</resources>
|
||||
23
SettingsLib/BarChartPreference/res/values/colors.xml
Normal file
23
SettingsLib/BarChartPreference/res/values/colors.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?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.
|
||||
-->
|
||||
|
||||
<resources>
|
||||
<color name="settings_bar_view_1_color">#A142F4</color>
|
||||
<color name="settings_bar_view_2_color">#24C1E0</color>
|
||||
<color name="settings_bar_view_3_color">#4285F4</color>
|
||||
<color name="settings_bar_view_4_color">#009688</color>
|
||||
</resources>
|
||||
21
SettingsLib/BarChartPreference/res/values/dimens.xml
Normal file
21
SettingsLib/BarChartPreference/res/values/dimens.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2018 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.
|
||||
-->
|
||||
|
||||
<resources>
|
||||
<dimen name="settings_bar_view_max_height">72dp</dimen>
|
||||
<dimen name="settings_bar_view_icon_size">24dp</dimen>
|
||||
</resources>
|
||||
108
SettingsLib/BarChartPreference/res/values/styles.xml
Normal file
108
SettingsLib/BarChartPreference/res/values/styles.xml
Normal file
@@ -0,0 +1,108 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2018 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.
|
||||
-->
|
||||
|
||||
<resources
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
tools:targetApi="28">
|
||||
|
||||
<style name="SettingsBarChart">
|
||||
<item name="android:layout_marginStart">10dp</item>
|
||||
<item name="android:layout_marginEnd">10dp</item>
|
||||
<item name="android:gravity">center</item>
|
||||
<item name="android:orientation">vertical</item>
|
||||
</style>
|
||||
|
||||
<style name="SettingsBarChartTitle">
|
||||
<item name="android:layout_width">wrap_content</item>
|
||||
<item name="android:layout_height">48dp</item>
|
||||
<item name="android:gravity">center</item>
|
||||
<item name="android:textAppearance">@style/BarChart.Text.HeaderTitle</item>
|
||||
</style>
|
||||
|
||||
<style name="SettingsBarChartDetailsButton"
|
||||
parent="@android:style/Widget.DeviceDefault.Button.Borderless.Colored">
|
||||
<item name="android:layout_width">wrap_content</item>
|
||||
<item name="android:layout_height">wrap_content</item>
|
||||
<item name="android:gravity">center</item>
|
||||
</style>
|
||||
|
||||
<style name="SettingsBarViewStyle">
|
||||
<item name="android:layout_width">0dp</item>
|
||||
<item name="android:layout_height">168dp</item>
|
||||
<item name="android:layout_weight">1</item>
|
||||
<item name="android:layout_marginStart">8dp</item>
|
||||
<item name="android:layout_marginEnd">8dp</item>
|
||||
</style>
|
||||
|
||||
<style name="SettingsBarChartEmptyText">
|
||||
<item name="android:layout_width">match_parent</item>
|
||||
<item name="android:layout_height">@dimen/settings_bar_view_max_height</item>
|
||||
<item name="android:gravity">center</item>
|
||||
<item name="android:visibility">gone</item>
|
||||
<item name="android:textAppearance">@style/BarChart.Text.Summary</item>
|
||||
</style>
|
||||
|
||||
<style name="SettingsBarChartBar">
|
||||
<item name="android:layout_width">8dp</item>
|
||||
</style>
|
||||
|
||||
<style name="SettingsBarChartBarIcon">
|
||||
<item name="android:layout_width">@dimen/settings_bar_view_icon_size</item>
|
||||
<item name="android:layout_height">@dimen/settings_bar_view_icon_size</item>
|
||||
<item name="android:scaleType">fitCenter</item>
|
||||
<item name="android:layout_marginTop">12dp</item>
|
||||
<item name="android:tint">?android:attr/textColorPrimary</item>
|
||||
</style>
|
||||
|
||||
<style name="SettingsBarChartBarTitle">
|
||||
<item name="android:layout_width">wrap_content</item>
|
||||
<item name="android:layout_height">wrap_content</item>
|
||||
<item name="android:layout_marginTop">12dp</item>
|
||||
<item name="android:singleLine">true</item>
|
||||
<item name="android:ellipsize">marquee</item>
|
||||
<item name="android:textAppearance">@style/BarChart.Text.Title</item>
|
||||
</style>
|
||||
|
||||
<style name="SettingsBarChartBarSummary">
|
||||
<item name="android:layout_width">wrap_content</item>
|
||||
<item name="android:layout_height">wrap_content</item>
|
||||
<item name="android:layout_marginTop">4dp</item>
|
||||
<item name="android:singleLine">true</item>
|
||||
<item name="android:ellipsize">marquee</item>
|
||||
<item name="android:textAppearance">@style/BarChart.Text.Summary</item>
|
||||
</style>
|
||||
|
||||
<style name="BarChart.Text"
|
||||
parent="@android:style/TextAppearance.DeviceDefault.WindowTitle">
|
||||
<item name="android:textColor">?android:attr/textColorPrimary</item>
|
||||
<item name="android:textSize">16sp</item>
|
||||
</style>
|
||||
|
||||
<style name="BarChart.Text.HeaderTitle">
|
||||
<item name="android:textSize">14sp</item>
|
||||
</style>
|
||||
|
||||
<style name="BarChart.Text.Title">
|
||||
<item name="android:textSize">14sp</item>
|
||||
</style>
|
||||
|
||||
<style name="BarChart.Text.Summary"
|
||||
parent="@android:style/TextAppearance.DeviceDefault">
|
||||
<item name="android:textColor">?android:attr/textColorSecondary</item>
|
||||
<item name="android:textSize">12sp</item>
|
||||
</style>
|
||||
</resources>
|
||||
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.android.settingslib.widget;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StringRes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.android.settingslib.widget.preference.barchart.R;
|
||||
/**
|
||||
* BarChartInfo is responsible for storing information about {@link BarChartPreference}.
|
||||
*/
|
||||
public class BarChartInfo {
|
||||
@StringRes
|
||||
private final int mTitle;
|
||||
@StringRes
|
||||
private final int mDetails;
|
||||
@StringRes
|
||||
private final int mEmptyText;
|
||||
private final View.OnClickListener mDetailsOnClickListener;
|
||||
|
||||
private BarViewInfo[] mBarViewInfos;
|
||||
|
||||
/**
|
||||
* Gets the resource id for the title shown in {@link BarChartPreference}.
|
||||
*
|
||||
* @return the string resource id for title.
|
||||
*/
|
||||
public int getTitle() {
|
||||
return mTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the resource id for the details shown in {@link BarChartPreference}.
|
||||
*
|
||||
* @return the string resource id for details.
|
||||
*/
|
||||
public int getDetails() {
|
||||
return mDetails;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the resource id for the empty text shown in {@link BarChartPreference} when there is no
|
||||
* any bar view in {@link BarChartPreference}.
|
||||
*
|
||||
* @return the string resource id for empty text.
|
||||
*/
|
||||
public int getEmptyText() {
|
||||
return mEmptyText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the click listener for the details view.
|
||||
*
|
||||
* @return click listener for details view.
|
||||
*/
|
||||
public View.OnClickListener getDetailsOnClickListener() {
|
||||
return mDetailsOnClickListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array which contains up to four {@link BarViewInfo}
|
||||
*
|
||||
* @return an array holding the current all {@link BarViewInfo} state of the bar chart.
|
||||
*/
|
||||
public BarViewInfo[] getBarViewInfos() {
|
||||
return mBarViewInfos;
|
||||
}
|
||||
|
||||
void setBarViewInfos(BarViewInfo[] barViewInfos) {
|
||||
mBarViewInfos = barViewInfos;
|
||||
}
|
||||
|
||||
private BarChartInfo(Builder builder) {
|
||||
mTitle = builder.mTitle;
|
||||
mDetails = builder.mDetails;
|
||||
mEmptyText = builder.mEmptyText;
|
||||
mDetailsOnClickListener = builder.mDetailsOnClickListener;
|
||||
|
||||
if (builder.mBarViewInfos != null) {
|
||||
mBarViewInfos = builder.mBarViewInfos.stream().toArray(BarViewInfo[]::new);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder class for {@link BarChartInfo}
|
||||
*/
|
||||
public static class Builder {
|
||||
@StringRes
|
||||
private int mTitle;
|
||||
@StringRes
|
||||
private int mDetails;
|
||||
@StringRes
|
||||
private int mEmptyText;
|
||||
private View.OnClickListener mDetailsOnClickListener;
|
||||
private List<BarViewInfo> mBarViewInfos;
|
||||
|
||||
/**
|
||||
* Creates an instance of a {@link BarChartInfo} based on the current builder settings.
|
||||
*
|
||||
* @return The {@link BarChartInfo}.
|
||||
*/
|
||||
public BarChartInfo build() {
|
||||
if (mTitle == 0) {
|
||||
throw new IllegalStateException("You must call Builder#setTitle() once.");
|
||||
}
|
||||
return new BarChartInfo(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the string resource id for the title.
|
||||
*/
|
||||
public Builder setTitle(@StringRes int title) {
|
||||
mTitle = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the string resource id for the details.
|
||||
*/
|
||||
public Builder setDetails(@StringRes int details) {
|
||||
mDetails = details;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the string resource id for the empty text.
|
||||
*/
|
||||
public Builder setEmptyText(@StringRes int emptyText) {
|
||||
mEmptyText = emptyText;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the click listener for details view.
|
||||
*/
|
||||
public Builder setDetailsOnClickListener(
|
||||
@Nullable View.OnClickListener clickListener) {
|
||||
mDetailsOnClickListener = clickListener;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a {@link BarViewInfo} for {@link BarChartPreference}.
|
||||
* Maximum of 4 {@link BarViewInfo} can be added.
|
||||
*/
|
||||
public Builder addBarViewInfo(@NonNull BarViewInfo barViewInfo) {
|
||||
if (mBarViewInfos == null) {
|
||||
mBarViewInfos = new ArrayList<>();
|
||||
}
|
||||
if (mBarViewInfos.size() >= BarChartPreference.MAXIMUM_BAR_VIEWS) {
|
||||
throw new IllegalStateException("We only support up to four bar views");
|
||||
}
|
||||
mBarViewInfos.add(barViewInfo);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.android.settingslib.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
import java.util.Arrays;
|
||||
import com.android.settingslib.widget.preference.barchart.R;
|
||||
/**
|
||||
* This BarChartPreference shows up to four bar views in this preference at most.
|
||||
*
|
||||
* <p>The following code sample shows a typical use, with an XML layout and code to initialize the
|
||||
* contents of the BarChartPreference:
|
||||
*
|
||||
* <pre>
|
||||
* <com.android.settingslib.widget.BarChartPreference
|
||||
* android:key="bar_chart"/>
|
||||
* </pre>
|
||||
*
|
||||
* <p>This code sample demonstrates how to initialize the contents of the BarChartPreference
|
||||
* defined in the previous XML layout:
|
||||
*
|
||||
* <pre>
|
||||
* BarChartPreference preference = ((BarChartPreference) findPreference("bar_chart"));
|
||||
*
|
||||
* BarChartInfo info = new BarChartInfo.Builder()
|
||||
* .setTitle(R.string.permission_bar_chart_title)
|
||||
* .setDetails(R.string.permission_bar_chart_details)
|
||||
* .setEmptyText(R.string.permission_bar_chart_empty_text)
|
||||
* .addBarViewInfo(new barViewInfo(...))
|
||||
* .addBarViewInfo(new barViewInfo(...))
|
||||
* .addBarViewInfo(new barViewInfo(...))
|
||||
* .addBarViewInfo(new barViewInfo(...))
|
||||
* .setDetailsOnClickListener(v -> doSomething())
|
||||
* .build();
|
||||
*
|
||||
* preference.initializeBarChart(info);
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* <p>You also can update new information for bar views by
|
||||
* {@link BarChartPreference#setBarViewInfos(BarViewInfo[])}
|
||||
*
|
||||
* <pre>
|
||||
* BarViewInfo[] barViewsInfo = new BarViewInfo [] {
|
||||
* new BarViewInfo(...),
|
||||
* new BarViewInfo(...),
|
||||
* new BarViewInfo(...),
|
||||
* new BarViewInfo(...),
|
||||
* };
|
||||
*
|
||||
* preference.setBarViewInfos(barViewsInfo);
|
||||
* </pre>
|
||||
*/
|
||||
public class BarChartPreference extends Preference {
|
||||
|
||||
public static final int MAXIMUM_BAR_VIEWS = 4;
|
||||
private static final String TAG = "BarChartPreference";
|
||||
private static final int[] BAR_VIEWS = {
|
||||
R.id.bar_view1,
|
||||
R.id.bar_view2,
|
||||
R.id.bar_view3,
|
||||
R.id.bar_view4
|
||||
};
|
||||
|
||||
private int mMaxBarHeight;
|
||||
private boolean mIsLoading;
|
||||
private BarChartInfo mBarChartInfo;
|
||||
|
||||
public BarChartPreference(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public BarChartPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public BarChartPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init();
|
||||
}
|
||||
|
||||
public BarChartPreference(Context context, AttributeSet attrs, int defStyleAttr,
|
||||
int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* According to the information in {@link BarChartInfo} to initialize bar chart.
|
||||
*
|
||||
* @param barChartInfo The barChartInfo contains title, details, empty text, click listener
|
||||
* attached on details view and four bar views.
|
||||
*/
|
||||
public void initializeBarChart(@NonNull BarChartInfo barChartInfo) {
|
||||
mBarChartInfo = barChartInfo;
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all bar view information which you'd like to show in preference.
|
||||
*
|
||||
* @param barViewInfos the barViewInfos contain at least one {@link BarViewInfo}.
|
||||
*/
|
||||
public void setBarViewInfos(@Nullable BarViewInfo[] barViewInfos) {
|
||||
if (barViewInfos != null && barViewInfos.length > MAXIMUM_BAR_VIEWS) {
|
||||
throw new IllegalStateException("We only support up to four bar views");
|
||||
}
|
||||
mBarChartInfo.setBarViewInfos(barViewInfos);
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set loading state for {@link BarChartPreference}.
|
||||
*
|
||||
* By default, {@link BarChartPreference} doesn't care about it.
|
||||
*
|
||||
* But if user sets loading state to true explicitly, it means {@link BarChartPreference}
|
||||
* needs to take some time to load data. So we won't initialize any view now.
|
||||
*
|
||||
* Once the state is updated to false, we will start to initialize view again.
|
||||
*
|
||||
* @param isLoading whether or not {@link BarChartPreference} is in loading state.
|
||||
*/
|
||||
public void updateLoadingState(boolean isLoading) {
|
||||
mIsLoading = isLoading;
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder holder) {
|
||||
super.onBindViewHolder(holder);
|
||||
holder.setDividerAllowedAbove(true);
|
||||
holder.setDividerAllowedBelow(true);
|
||||
|
||||
// We bind title and details early so that we can preserve the correct height for chart
|
||||
// view.
|
||||
bindChartTitleView(holder);
|
||||
bindChartDetailsView(holder);
|
||||
|
||||
// If the state is loading, we just show a blank view.
|
||||
if (mIsLoading) {
|
||||
holder.itemView.setVisibility(View.INVISIBLE);
|
||||
return;
|
||||
}
|
||||
holder.itemView.setVisibility(View.VISIBLE);
|
||||
|
||||
final BarViewInfo[] barViewInfos = mBarChartInfo.getBarViewInfos();
|
||||
// If there is no any bar view, we just show an empty text.
|
||||
if (barViewInfos == null || barViewInfos.length == 0) {
|
||||
setEmptyViewVisible(holder, true /* visible */);
|
||||
return;
|
||||
}
|
||||
setEmptyViewVisible(holder, false /* visible */);
|
||||
updateBarChart(holder);
|
||||
}
|
||||
|
||||
private void init() {
|
||||
setSelectable(false);
|
||||
setLayoutResource(R.layout.settings_bar_chart);
|
||||
mMaxBarHeight = getContext().getResources().getDimensionPixelSize(
|
||||
R.dimen.settings_bar_view_max_height);
|
||||
}
|
||||
|
||||
private void bindChartTitleView(PreferenceViewHolder holder) {
|
||||
final TextView titleView = (TextView) holder.findViewById(R.id.bar_chart_title);
|
||||
titleView.setText(mBarChartInfo.getTitle());
|
||||
}
|
||||
|
||||
private void bindChartDetailsView(PreferenceViewHolder holder) {
|
||||
final Button detailsView = (Button) holder.findViewById(R.id.bar_chart_details);
|
||||
final int details = mBarChartInfo.getDetails();
|
||||
if (details == 0) {
|
||||
detailsView.setVisibility(View.GONE);
|
||||
} else {
|
||||
detailsView.setVisibility(View.VISIBLE);
|
||||
detailsView.setText(details);
|
||||
detailsView.setOnClickListener(mBarChartInfo.getDetailsOnClickListener());
|
||||
}
|
||||
}
|
||||
|
||||
private void updateBarChart(PreferenceViewHolder holder) {
|
||||
normalizeBarViewHeights();
|
||||
|
||||
final BarViewInfo[] barViewInfos = mBarChartInfo.getBarViewInfos();
|
||||
|
||||
for (int index = 0; index < MAXIMUM_BAR_VIEWS; index++) {
|
||||
final BarView barView = (BarView) holder.findViewById(BAR_VIEWS[index]);
|
||||
|
||||
// If there is no bar view info can be shown.
|
||||
if (barViewInfos == null || index >= barViewInfos.length) {
|
||||
barView.setVisibility(View.GONE);
|
||||
continue;
|
||||
}
|
||||
barView.setVisibility(View.VISIBLE);
|
||||
barView.updateView(barViewInfos[index]);
|
||||
}
|
||||
}
|
||||
|
||||
private void normalizeBarViewHeights() {
|
||||
final BarViewInfo[] barViewInfos = mBarChartInfo.getBarViewInfos();
|
||||
// If there is no any bar view info, we don't need to calculate the height of all bar views.
|
||||
if (barViewInfos == null || barViewInfos.length == 0) {
|
||||
return;
|
||||
}
|
||||
// Do a sort in descending order, the first element would have max {@link
|
||||
// BarViewInfo#mHeight}
|
||||
Arrays.sort(barViewInfos);
|
||||
// Since we sorted this array in advance, the first element must have the max {@link
|
||||
// BarViewInfo#mHeight}.
|
||||
final int maxBarHeight = barViewInfos[0].getHeight();
|
||||
// If the max number of bar view is zero, then we don't calculate the unit for bar height.
|
||||
final int unit = maxBarHeight == 0 ? 0 : mMaxBarHeight / maxBarHeight;
|
||||
|
||||
for (BarViewInfo barView : barViewInfos) {
|
||||
barView.setNormalizedHeight(barView.getHeight() * unit);
|
||||
}
|
||||
}
|
||||
|
||||
private void setEmptyViewVisible(PreferenceViewHolder holder, boolean visible) {
|
||||
final View barViewsContainer = holder.findViewById(R.id.bar_views_container);
|
||||
final TextView emptyView = (TextView) holder.findViewById(R.id.empty_view);
|
||||
final int emptyTextRes = mBarChartInfo.getEmptyText();
|
||||
|
||||
if (emptyTextRes != 0) {
|
||||
emptyView.setText(emptyTextRes);
|
||||
}
|
||||
emptyView.setVisibility(visible ? View.VISIBLE : View.GONE);
|
||||
barViewsContainer.setVisibility(visible ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.settingslib.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import com.android.settingslib.widget.preference.barchart.R;
|
||||
/**
|
||||
* {@link View} for a single vertical bar with icon and summary.
|
||||
*/
|
||||
public class BarView extends LinearLayout {
|
||||
|
||||
private static final String TAG = "BarView";
|
||||
|
||||
private View mBarView;
|
||||
private ImageView mIcon;
|
||||
private TextView mBarTitle;
|
||||
private TextView mBarSummary;
|
||||
|
||||
public BarView(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public BarView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
|
||||
// Get accent color
|
||||
TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.colorAccent});
|
||||
@ColorInt final int colorAccent = a.getColor(0, 0);
|
||||
|
||||
// Get bar color from layout XML
|
||||
a = context.obtainStyledAttributes(attrs, R.styleable.SettingsBarView);
|
||||
@ColorInt final int barColor = a.getColor(R.styleable.SettingsBarView_barColor,
|
||||
colorAccent);
|
||||
a.recycle();
|
||||
|
||||
mBarView.setBackgroundColor(barColor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the view with a {@link BarViewInfo}.
|
||||
*/
|
||||
void updateView(BarViewInfo barViewInfo) {
|
||||
setOnClickListener(barViewInfo.getClickListener());
|
||||
//Set height of bar view
|
||||
mBarView.getLayoutParams().height = barViewInfo.getNormalizedHeight();
|
||||
mIcon.setImageDrawable(barViewInfo.getIcon());
|
||||
mBarTitle.setText(barViewInfo.getTitle());
|
||||
mBarSummary.setText(barViewInfo.getSummary());
|
||||
|
||||
final CharSequence barViewInfoContent = barViewInfo.getContentDescription();
|
||||
if (!TextUtils.isEmpty(barViewInfoContent)
|
||||
&& !TextUtils.equals((barViewInfo.getTitle()), barViewInfoContent)) {
|
||||
mIcon.setContentDescription(barViewInfo.getContentDescription());
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
CharSequence getTitle() {
|
||||
return mBarTitle.getText();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
CharSequence getSummary() {
|
||||
return mBarSummary.getText();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
LayoutInflater.from(getContext()).inflate(R.layout.settings_bar_view, this);
|
||||
setOrientation(LinearLayout.VERTICAL);
|
||||
setGravity(Gravity.CENTER | Gravity.BOTTOM);
|
||||
|
||||
mBarView = findViewById(R.id.bar_view);
|
||||
mIcon = findViewById(R.id.icon_view);
|
||||
mBarTitle = findViewById(R.id.bar_title);
|
||||
mBarSummary = findViewById(R.id.bar_summary);
|
||||
}
|
||||
|
||||
private void setOnClickListner(View.OnClickListener listener) {
|
||||
mBarView.setOnClickListener(listener);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.settingslib.widget;
|
||||
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.IntRange;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.util.Comparator;
|
||||
import com.android.settingslib.widget.preference.barchart.R;
|
||||
/**
|
||||
* A class responsible for saving bar view information.
|
||||
*/
|
||||
public class BarViewInfo implements Comparable<BarViewInfo> {
|
||||
|
||||
private final Drawable mIcon;
|
||||
private View.OnClickListener mClickListener;
|
||||
private CharSequence mTitle;
|
||||
private CharSequence mSummary;
|
||||
private @Nullable CharSequence mContentDescription;
|
||||
// A number indicates this bar's height. The larger number shows a higher bar view.
|
||||
private int mHeight;
|
||||
// A real height of bar view.
|
||||
private int mNormalizedHeight;
|
||||
|
||||
/**
|
||||
* Construct a BarViewInfo instance.
|
||||
*
|
||||
* @param icon The icon of bar view.
|
||||
* @param barHeight The height of bar view. Larger number shows a higher bar view.
|
||||
* @param title The string for title. If this is null, use the height of the bar.
|
||||
* @param summary The string for summary.
|
||||
* @param contentDescription Optional text that briefly describes the contents of the icon.
|
||||
*/
|
||||
public BarViewInfo(Drawable icon, @IntRange(from = 0) int barHeight,
|
||||
@Nullable CharSequence title, CharSequence summary,
|
||||
@Nullable CharSequence contentDescription) {
|
||||
mIcon = icon;
|
||||
mHeight = barHeight;
|
||||
mTitle = title;
|
||||
mSummary = summary;
|
||||
mContentDescription = contentDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a click listener for bar view.
|
||||
*/
|
||||
public void setClickListener(@Nullable View.OnClickListener listener) {
|
||||
mClickListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(BarViewInfo other) {
|
||||
// Descending order
|
||||
return Comparator.comparingInt((BarViewInfo barViewInfo) -> barViewInfo.mHeight)
|
||||
.compare(other, this);
|
||||
}
|
||||
|
||||
void setHeight(@IntRange(from = 0) int height) {
|
||||
mHeight = height;
|
||||
}
|
||||
|
||||
void setTitle(CharSequence title) {
|
||||
mTitle = title;
|
||||
}
|
||||
|
||||
void setSummary(CharSequence summary) {
|
||||
mSummary = summary;
|
||||
}
|
||||
|
||||
Drawable getIcon() {
|
||||
return mIcon;
|
||||
}
|
||||
|
||||
int getHeight() {
|
||||
return mHeight;
|
||||
}
|
||||
|
||||
View.OnClickListener getClickListener() {
|
||||
return mClickListener;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
CharSequence getTitle() {
|
||||
return mTitle;
|
||||
}
|
||||
|
||||
CharSequence getSummary() {
|
||||
return mSummary;
|
||||
}
|
||||
|
||||
public @Nullable CharSequence getContentDescription() {
|
||||
return mContentDescription;
|
||||
}
|
||||
|
||||
void setNormalizedHeight(@IntRange(from = 0) int barHeight) {
|
||||
mNormalizedHeight = barHeight;
|
||||
}
|
||||
|
||||
int getNormalizedHeight() {
|
||||
return mNormalizedHeight;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user