Commit 11b6987c authored by Bartek Fabiszewski's avatar Bartek Fabiszewski
Browse files

Allow to manually set minimum time, distance and accuracy values, closes #18

parent c8c45cee
Loading
Loading
Loading
Loading
+120 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2017 Bartek Fabiszewski
 * http://www.fabiszewski.net
 *
 * This file is part of μlogger-android.
 * Licensed under GPL, either version 3, or any later.
 * See <http://www.gnu.org/licenses/>
 */

package net.fabiszewski.ulogger;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.os.Build;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static net.fabiszewski.ulogger.Alert.showAlert;

class ListPreferenceWithEditText extends ListPreference implements Preference.OnPreferenceChangeListener {

    private CharSequence otherSummary;

    private static final String TAG = ListPreferenceWithEditText.class.getSimpleName();
    private static final String OTHER = "other";

    public ListPreferenceWithEditText(Context context) {
        super(context);
        init(context, null, 0, 0);
    }

    public ListPreferenceWithEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0, 0);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ListPreferenceWithEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, 0);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ListPreferenceWithEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        setOnPreferenceChangeListener(this);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ListPreferenceWithEditText, defStyleAttr, defStyleRes);
        otherSummary = a.getText(R.styleable.ListPreferenceWithEditText_otherSummary);
        a.recycle();
    }

    @Override
    public int findIndexOfValue(String value) {
        int index = super.findIndexOfValue(value);
        if (index == -1) {
            return super.findIndexOfValue(OTHER);
        } else {
            return index;
        }
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (newValue.toString().equals(OTHER)) {
            showOtherDialog(preference.getContext(), preference);
            return false;
        }
        return true;
    }

    private void showOtherDialog(Context context, Preference preference) {
        final String key = preference.getKey();
        final AlertDialog dialog = showAlert(context,
                preference.getTitle(),
                R.layout.other_dialog);
        final TextView textView = (TextView) dialog.findViewById(R.id.other_textview);
        textView.setText(otherSummary);
        final EditText editText = (EditText) dialog.findViewById(R.id.other_edittext);
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        editText.setText(prefs.getString(key, ""));
        final Button submit = (Button) dialog.findViewById(R.id.other_button_submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newval = editText.getText().toString();
                if (newval.length() > 0 && Integer.valueOf(newval) >= 0) {
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putString(key, newval);
                    editor.apply();
                    setValue(newval);
                    if (Logger.DEBUG) { Log.d(TAG, "[" + key + " set to " + newval + "]"); }
                }
                dialog.cancel();
            }
        });

        final Button cancel = (Button) dialog.findViewById(R.id.other_button_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
    }

}
+46 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (c) 2017 Bartek Fabiszewski
  ~ http://www.fabiszewski.net
  ~
  ~ This file is part of μlogger-android.
  ~ Licensed under GPL, either version 3, or any later.
  ~ See <http://www.gnu.org/licenses/>
  -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dip">

    <TextView
        android:id="@+id/other_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <EditText
        android:id="@+id/other_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10" />

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/other_button_cancel"
            android:text="@string/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/other_button_submit"
            android:text="@string/submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>
</LinearLayout>
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
        <item>15 minutes</item>
        <item>30 minutes</item>
        <item>1 heure</item>
        <item>autre</item>
    </string-array>

    <string-array name="minDistanceEntries">
@@ -27,6 +28,7 @@
        <item>500 m</item>
        <item>1 km</item>
        <item>5 km</item>
        <item>autre</item>
    </string-array>

    <string-array name="minAccuracyEntries">
@@ -38,6 +40,7 @@
        <item>500 m</item>
        <item>1 km</item>
        <item>5 km</item>
        <item>autre</item>
    </string-array>

    <string-array name="providersEntries">
+3 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
        <item>15 分</item>
        <item>30 分</item>
        <item>1 時間</item>
        <item>別の</item>
    </string-array>

    <string-array name="minDistanceEntries">
@@ -27,6 +28,7 @@
        <item>500 m</item>
        <item>1 km</item>
        <item>5 km</item>
        <item>別の</item>
    </string-array>

    <string-array name="minAccuracyEntries">
@@ -38,6 +40,7 @@
        <item>500 m</item>
        <item>1 km</item>
        <item>5 km</item>
        <item>別の</item>
    </string-array>

    <string-array name="providersEntries">
+3 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
        <item>15 minut</item>
        <item>30 minut</item>
        <item>1 godzina</item>
        <item>inny</item>
    </string-array>

    <string-array name="minDistanceEntries">
@@ -27,6 +28,7 @@
        <item>500 m</item>
        <item>1 km</item>
        <item>5 km</item>
        <item>inna</item>
    </string-array>

    <string-array name="minAccuracyEntries">
@@ -38,6 +40,7 @@
        <item>500 m</item>
        <item>1 km</item>
        <item>5 km</item>
        <item>inna</item>
    </string-array>

    <string-array name="providersEntries">
Loading