Verified Commit c0b227d9 authored by Marvin W.'s avatar Marvin W. 🐿
Browse files

Add dashboard and condition functionality

parent ea80c3d0
......@@ -34,6 +34,7 @@ import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public abstract class AbstractAboutFragment extends Fragment {
......@@ -145,7 +146,7 @@ public abstract class AbstractAboutFragment extends Fragment {
@Override
public int compareTo(Library another) {
return name.toLowerCase().compareTo(another.name.toLowerCase());
return name.toLowerCase(Locale.US).compareTo(another.name.toLowerCase(Locale.US));
}
}
}
package org.microg.tools.ui;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractDashboardActivity extends AppCompatActivity {
protected int preferencesResource = 0;
private final List<Condition> conditions = new ArrayList<Condition>();
private ViewGroup conditionContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_activity);
conditionContainer = (ViewGroup) findViewById(R.id.condition_container);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_wrapper, getFragment())
.commit();
}
@Override
protected void onResume() {
super.onResume();
forceConditionReevaluation();
}
private synchronized void resetConditionViews() {
conditionContainer.removeAllViews();
for (Condition condition : conditions) {
if (condition.isEvaluated()) {
if (condition.isActive(this)) {
addConditionToView(condition);
}
} else {
evaluateConditionAsync(condition);
}
}
}
private void evaluateConditionAsync(final Condition condition) {
if (condition.willBeEvaluating()) {
new Thread(new Runnable() {
@Override
public void run() {
if (condition.isActive(AbstractDashboardActivity.this)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (conditions.contains(condition) && condition.isEvaluated()) {
addConditionToView(condition);
}
}
});
}
}
}).start();
}
}
protected void forceConditionReevaluation() {
for (Condition condition : conditions) {
condition.resetEvaluated();
}
resetConditionViews();
}
protected void addAllConditions(Condition[] conditions) {
for (Condition condition : conditions) {
addCondition(condition);
}
}
protected void addCondition(Condition condition) {
conditions.add(condition);
if (conditionContainer == null) return;
if (condition.isEvaluated()) {
addConditionToView(condition);
} else {
evaluateConditionAsync(condition);
}
}
private synchronized void addConditionToView(Condition condition) {
for (int i = 0; i < conditionContainer.getChildCount(); i++) {
if (conditionContainer.getChildAt(i).getTag() == condition) return;
}
conditionContainer.addView(condition.createView(this, conditionContainer));
}
protected void clearConditions() {
conditions.clear();
resetConditionViews();
}
protected Fragment getFragment() {
if (preferencesResource == 0) {
throw new IllegalStateException("Neither preferencesResource given, nor overriden getFragment()");
}
ResourceSettingsFragment fragment = new ResourceSettingsFragment();
Bundle b = new Bundle();
b.putInt(ResourceSettingsFragment.EXTRA_PREFERENCE_RESOURCE, preferencesResource);
fragment.setArguments(b);
return fragment;
}
}
......@@ -8,7 +8,7 @@ import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.ViewGroup;
public class AbstractSettingsActivity extends AppCompatActivity {
public abstract class AbstractSettingsActivity extends AppCompatActivity {
protected boolean showHomeAsUp = false;
protected int preferencesResource = 0;
private ViewGroup customBarContainer;
......
/*
* Copyright (C) 2013-2016 microG Project Team
*
* 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 org.microg.tools.ui;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v4.content.res.ResourcesCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Condition {
@DrawableRes
private final int iconRes;
private final Drawable icon;
@StringRes
private final int titleRes;
private final CharSequence title;
@StringRes
private final int summaryRes;
private final CharSequence summary;
@StringRes
private final int firstActionTextRes;
private final CharSequence firstActionText;
private final View.OnClickListener firstActionListener;
@StringRes
private final int secondActionTextRes;
private final CharSequence secondActionText;
private final View.OnClickListener secondActionListener;
private final Evaluation evaluation;
private boolean evaluated = false;
private boolean evaluating = false;
private boolean active;
Condition(Builder builder) {
icon = builder.icon;
title = builder.title;
summary = builder.summary;
firstActionText = builder.firstActionText;
firstActionListener = builder.firstActionListener;
secondActionText = builder.secondActionText;
secondActionListener = builder.secondActionListener;
summaryRes = builder.summaryRes;
iconRes = builder.iconRes;
firstActionTextRes = builder.firstActionTextRes;
secondActionTextRes = builder.secondActionTextRes;
titleRes = builder.titleRes;
evaluation = builder.evaluation;
}
View createView(final Context context, ViewGroup container) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.condition_card, container, false);
Drawable icon = getIcon(context);
if (icon != null)
((ImageView) view.findViewById(android.R.id.icon)).setImageDrawable(icon);
((TextView) view.findViewById(android.R.id.title)).setText(getTitle(context));
((TextView) view.findViewById(android.R.id.summary)).setText(getSummary(context));
Button first = (Button) view.findViewById(R.id.first_action);
first.setText(getFirstActionText(context));
first.setOnClickListener(getFirstActionListener());
CharSequence secondActionText = getSecondActionText(context);
if (secondActionText != null) {
Button second = (Button) view.findViewById(R.id.second_action);
second.setText(secondActionText);
second.setOnClickListener(getSecondActionListener());
second.setVisibility(View.VISIBLE);
}
final View detailGroup = view.findViewById(R.id.detail_group);
final ImageView expandIndicator = (ImageView) view.findViewById(R.id.expand_indicator);
View.OnClickListener expandListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (detailGroup.getVisibility() == View.VISIBLE) {
expandIndicator.setImageDrawable(ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_expand_more, context.getTheme()));
detailGroup.setVisibility(View.GONE);
} else {
expandIndicator.setImageDrawable(ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_expand_less, context.getTheme()));
detailGroup.setVisibility(View.VISIBLE);
}
}
};
view.findViewById(R.id.collapsed_group).setOnClickListener(expandListener);
expandIndicator.setOnClickListener(expandListener);
view.setTag(this);
return view;
}
public Drawable getIcon(Context context) {
if (iconRes != 0) {
return ResourcesCompat.getDrawable(context.getResources(), iconRes, context.getTheme());
}
return icon;
}
public CharSequence getTitle(Context context) {
if (titleRes != 0) {
return context.getString(titleRes);
}
return title;
}
public CharSequence getSummary(Context context) {
if (summaryRes != 0) {
return context.getString(summaryRes);
}
return summary;
}
public View.OnClickListener getFirstActionListener() {
return firstActionListener;
}
public CharSequence getFirstActionText(Context context) {
if (firstActionTextRes != 0) {
return context.getString(firstActionTextRes);
}
return firstActionText;
}
public View.OnClickListener getSecondActionListener() {
return secondActionListener;
}
public CharSequence getSecondActionText(Context context) {
if (secondActionTextRes != 0) {
return context.getString(secondActionTextRes);
}
return secondActionText;
}
public synchronized boolean willBeEvaluating() {
if (!evaluating && !evaluated && evaluation != null) {
return evaluating = true;
} else {
return false;
}
}
public boolean isEvaluated() {
return evaluated || evaluation == null;
}
public synchronized void evaluate(Context context) {
active = evaluation == null || evaluation.isActive(context);
evaluated = true;
evaluating = false;
}
public boolean isActive(Context context) {
if (!evaluated && evaluation != null) evaluate(context);
return active;
}
public void resetEvaluated() {
this.evaluated = false;
}
public interface Evaluation {
boolean isActive(Context context);
}
public static class Builder {
@DrawableRes
private int iconRes;
private Drawable icon;
@StringRes
private int titleRes;
private CharSequence title;
@StringRes
private int summaryRes;
private CharSequence summary;
@StringRes
private int firstActionTextRes;
private CharSequence firstActionText;
private View.OnClickListener firstActionListener;
@StringRes
private int secondActionTextRes;
private CharSequence secondActionText;
private View.OnClickListener secondActionListener;
private Evaluation evaluation;
public Builder() {
}
public Builder icon(Drawable val) {
icon = val;
return this;
}
public Builder icon(@DrawableRes int val) {
iconRes = val;
return this;
}
public Builder title(CharSequence val) {
title = val;
return this;
}
public Builder title(@StringRes int val) {
titleRes = val;
return this;
}
public Builder summary(CharSequence val) {
summary = val;
return this;
}
public Builder summary(@StringRes int val) {
summaryRes = val;
return this;
}
public Builder firstAction(CharSequence text, View.OnClickListener listener) {
firstActionText = text;
firstActionListener = listener;
return this;
}
public Builder firstAction(@StringRes int val, View.OnClickListener listener) {
firstActionTextRes = val;
firstActionListener = listener;
return this;
}
public Builder secondAction(CharSequence text, View.OnClickListener listener) {
secondActionText = text;
secondActionListener = listener;
return this;
}
public Builder secondAction(@StringRes int val, View.OnClickListener listener) {
secondActionTextRes = val;
secondActionListener = listener;
return this;
}
public Builder evaluation(Evaluation evaluation) {
this.evaluation = evaluation;
return this;
}
public Condition build() {
return new Condition(this);
}
}
}
......@@ -30,7 +30,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import java.util.ArrayList;
......@@ -208,8 +207,8 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
*/
private SavedState(Parcel in) {
super(in);
checked = (Boolean) in.readValue(null);
visible = (Boolean) in.readValue(null);
checked = (Boolean) in.readValue(Boolean.class.getClassLoader());
visible = (Boolean) in.readValue(Boolean.class.getClassLoader());
}
@Override
......
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2013-2016 microG Project Team
~
~ 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.
-->
<selector/>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 The Android Open Source Project
~ Copyright (C) 2015-2016 microG Project Team
~
~ 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportHeight="24"
android:viewportWidth="24">
<path
android:fillColor="@android:color/white"
android:pathData="M12.0,8.0l-6.0,6.0 1.41,1.41L12.0,10.83l4.59,4.58L18.0,14.0z"/>
</vector>
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 The Android Open Source Project
~ Copyright (C) 2015-2016 microG Project Team
~
~ 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportHeight="24"
android:viewportWidth="24">
<path
android:fillColor="@android:color/white"
android:pathData="M16.59,8.59L12.0,13.17 7.41,8.59 6.0,10.0l6.0,6.0 6.0,-6.0z"/>
</vector>
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2013-2016 microG Project Team
~
~ 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
......
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2013-2016 microG Project Team
~
~ 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.
-->
<color xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/switchbar_background_color"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider"/>
<TextView android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:paddingBottom="8dip"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"