Skip to content

Commit

Permalink
help
Browse files Browse the repository at this point in the history
adds data source help text; links to online help (#223)
  • Loading branch information
forrestguice committed Jan 16, 2022
1 parent fb9f106 commit af1cb66
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
Copyright (C) 2022 Forrest Guice
This file is part of SuntimesWidget.
SuntimesWidget is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SuntimesWidget is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SuntimesWidget. If not, see <http://www.gnu.org/licenses/>.
*/

package com.forrestguice.suntimeswidget.settings;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.net.Uri;
import android.preference.DialogPreference;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.forrestguice.suntimeswidget.R;
import com.forrestguice.suntimeswidget.SuntimesUtils;

/**
* A "preference" (non-persistent) that displays help content in a dialog.
*/
@TargetApi(11)
public class HelpPreference extends DialogPreference
{
private CharSequence helpText = "";
private String helpLink = null;
private TextView helpView;

@TargetApi(21)
public HelpPreference(Context context) {
super(context);
}

public HelpPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setHelpText(context, attrs);
}

@TargetApi(21)
public HelpPreference(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
setHelpText(context, attrs);
}

@TargetApi(21)
public HelpPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
setHelpText(context, attrs);
}

@Override
protected View onCreateDialogView()
{
Context context = getContext();

float marginTopBottom = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getContext().getResources().getDisplayMetrics());
float marginLeftRight;
TypedArray a = context.obtainStyledAttributes(new int[] { R.attr.dialogPreferredPadding });
marginLeftRight = context.getResources().getDimension(a.getResourceId(0, R.dimen.settingsGroup_margin));
a.recycle();

helpView = new TextView(getContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params1.gravity = Gravity.START;
params1.setMargins((int)marginLeftRight, (int)marginTopBottom, (int)marginLeftRight, (int)marginTopBottom);
helpView.setLayoutParams(params1);

LinearLayout dialogView = new LinearLayout(getContext());
dialogView.setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
dialogView.setLayoutParams(layoutParams);

dialogView.addView(helpView);
return dialogView;
}

@Override
protected void onBindDialogView(View v)
{
super.onBindDialogView(v);
helpView.setText(helpText);
}

@Override
protected void onDialogClosed(boolean result)
{
if (result && (getPositiveButtonText() != null && helpLink != null)) {
openHelpLink(getContext());
}
}

@Override
protected Object onGetDefaultValue(TypedArray a, int i) {
return a.getInt(i, 0);
}

@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
}

public void setHelpText(Context context, @Nullable AttributeSet attrs)
{
if (attrs != null)
{
String buttonText = null;
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HelpPreference, 0, 0);
try {
this.helpText = SuntimesUtils.fromHtml(a.getString(R.styleable.HelpPreference_helpText));
helpLink = a.getString(R.styleable.HelpPreference_helpLink);
buttonText = a.getString(R.styleable.HelpPreference_moreHelpButtonText);
} finally {
a.recycle();
}
setPositiveButtonText(buttonText);
}
}
public CharSequence getHelpText() {
return helpText;
}

public void openHelpLink(Context context)
{
if (context != null && helpLink != null) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(helpLink)));
}
}
public String getHelpLink() {
return helpLink;
}

}
6 changes: 6 additions & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,10 @@
<attr name="zeroValueText" format="string" />
</declare-styleable>

<declare-styleable name="HelpPreference">
<attr name="helpText" format="string" />
<attr name="helpLink" format="string" />
<attr name="moreHelpButtonText" format="string" />
</declare-styleable>

</resources>
14 changes: 14 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<string name="configAction_worldMap">World Map</string>
<string name="configAction_moon">Moon</string>
<string name="configAction_help">Help</string>
<string name="configAction_onlineHelp">Online Help</string> <!-- TODO -->
<string name="configAction_overflow">More</string>
<string name="configAction_settings">Settings</string>
<string name="configAction_aboutWidget">About</string>
Expand Down Expand Up @@ -289,6 +290,7 @@
<string name="configLabel_general_calculator">Data Source:</string> <!-- spinner label -->
<string name="configLabel_general_calculator_sun">Sun Data Source</string> <!-- preference label -->
<string name="configLabel_general_calculator_moon">Moon Data Source</string> <!-- preference label -->
<string name="configLabel_general_calculator_help">About Data Sources</string> <!-- preference title / help button text --> <!-- TODO -->

<string name="configLabel_general_timeFormatMode">Time Format:</string> <!-- spinner label -->

Expand Down Expand Up @@ -1870,6 +1872,18 @@
<string name="calculator_displayString_time4a_noaa">Follows algorithms published by the National Oceanic and Atmospheric Administration.</string>
<string name="calculator_displayString_time4a_cc">Follows algorithms published by Dershowitz/Reingold in their book "Calendrical Calculations" (third edition).</string>
<string name="calculator_displayString_time4a_4j">Based on astronomical calculations published by Jean Meeus in his book "Astronomical Algorithms" (second edition).</string>
<string name="help_datasources">
<![CDATA[
Suntimes uses third-party libraries to perform astronomical calculations.<br/>
<br/>
The <b>data source</b> setting allows choosing between different libraries (or choosing between different algorithms offered by those libraries). The default is <b>time4a-time4j</b>.<br/>
<br/>
This setting affects the speed and accuracy of calculations, and may limit which features are available. <br/>
<br/>
For more information about available choices, or extending the app to support other libraries, see the online help.
]]>
</string> <!-- TODO -->
<string name="help_datasources_url">https://github.com/forrestguice/SuntimesWidget/wiki/Help</string>

<!-- pref defaults -->
<string name="def_appwidget_0_general_calculator" translatable="false">time4a-time4j</string>
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/xml/preference_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
android:dialogTitle="@string/configLabel_general_calculator_moon"
android:summary="%s"
android:entries="@null" android:entryValues="@null" android:defaultValue="@string/def_appwidget_0_general_calculator_moon" />

<com.forrestguice.suntimeswidget.settings.HelpPreference
android:key="appwidget_0_general_calculator_help"
android:persistent="false"
android:title="@string/configLabel_general_calculator_help"
android:icon="?attr/icActionHelp"
app:helpText="@string/help_datasources"
app:helpLink="@string/help_datasources_url"
app:moreHelpButtonText="@string/configAction_onlineHelp"
/>

</PreferenceCategory>

<!--<PreferenceCategory
Expand Down

0 comments on commit af1cb66

Please sign in to comment.