Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long click to modify the history record name #161

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/src/main/java/com/zcshou/database/DataBaseHistoryLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@ public static void saveHistoryLocation(SQLiteDatabase sqLiteDatabase, ContentVal
XLog.e("DATABASE: insert error");
}
}

// 修改历史记录名称
public static void updateHistoryLocation(SQLiteDatabase sqLiteDatabase, String locID, String location) {
try{
ContentValues contentValues = new ContentValues();
contentValues.put(DB_COLUMN_LOCATION, location);
sqLiteDatabase.update(TABLE_NAME, contentValues, DB_COLUMN_ID + " = ?", new String[]{locID});
} catch (Exception e){
XLog.e("DATABASE: update error");
}
}
}
104 changes: 31 additions & 73 deletions app/src/main/java/com/zcshou/gogogo/FragmentSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.os.Bundle;
import android.text.InputType;
import android.text.Selection;

import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
Expand All @@ -17,6 +16,25 @@

public class FragmentSettings extends PreferenceFragmentCompat {

// Set a non-empty decimal EditTextPreference
private void setupDecimalEditTextPreference(EditTextPreference preference) {
if (preference != null) {
preference.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) pref ->
getResources().getString(R.string.setting_current_value) + pref.getText());
preference.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
editText.setSelection(editText.length());
});
preference.setOnPreferenceChangeListener((pref, newValue) -> {
if (newValue.toString().trim().isEmpty()) {
GoUtils.DisplayToast(this.getContext(), getResources().getString(R.string.app_error_input_null));
return false;
}
return true;
});
}
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// Load the preferences from an XML resource
Expand All @@ -38,68 +56,16 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
}

EditTextPreference pfWalk = findPreference("setting_walk");
if (pfWalk != null) {
// 使用自定义 SummaryProvider
pfWalk.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> getResources().getString(R.string.setting_current_value) + preference.getText());
pfWalk.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
Selection.setSelection(editText.getText(), editText.length());
});
pfWalk.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().trim().length() == 0) {
GoUtils.DisplayToast(this.getContext(),getResources().getString(R.string.app_error_input_null));
return false;
}
return true;
});
}
setupDecimalEditTextPreference(pfWalk);

EditTextPreference pfRun = findPreference("setting_run");
if (pfRun != null) {
pfRun.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> getResources().getString(R.string.setting_current_value) + preference.getText());
pfRun.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
Selection.setSelection(editText.getText(), editText.length());
});
pfRun.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().trim().length() == 0) {
GoUtils.DisplayToast(this.getContext(),getResources().getString(R.string.app_error_input_null));
return false;
}
return true;
});
}
setupDecimalEditTextPreference(pfRun);

EditTextPreference pfBike = findPreference("setting_bike");
if (pfBike != null) {
pfBike.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> getResources().getString(R.string.setting_current_value) + preference.getText());
pfBike.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
Selection.setSelection(editText.getText(), editText.length());
});
pfBike.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().trim().length() == 0) {
GoUtils.DisplayToast(this.getContext(),getResources().getString(R.string.app_error_input_null));
return false;
}
return true;
});
}
setupDecimalEditTextPreference(pfBike);

EditTextPreference pfAltitude = findPreference("setting_altitude");
if (pfAltitude != null) {
pfAltitude.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> getResources().getString(R.string.setting_current_value) + preference.getText());
pfAltitude.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
Selection.setSelection(editText.getText(), editText.length());
});
pfAltitude.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().trim().length() == 0) {
GoUtils.DisplayToast(this.getContext(),getResources().getString(R.string.app_error_input_null));
return false;
}
return true;
});
}
setupDecimalEditTextPreference(pfAltitude);

SwitchPreferenceCompat pLog = findPreference("setting_log_off");
if (pLog != null) {
Expand All @@ -120,20 +86,12 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
}

EditTextPreference pfPosHisValid = findPreference("setting_pos_history");
if (pfPosHisValid != null) {
// 使用自定义 SummaryProvider
pfPosHisValid.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> getResources().getString(R.string.setting_current_value) + preference.getText());
pfPosHisValid.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
Selection.setSelection(editText.getText(), editText.length());
});
pfPosHisValid.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().trim().length() == 0) {
GoUtils.DisplayToast(this.getContext(),getResources().getString(R.string.app_error_input_null));
return false;
}
return true;
});
}
setupDecimalEditTextPreference(pfPosHisValid);

EditTextPreference pfLatOffset = findPreference("setting_lat_max_offset");
setupDecimalEditTextPreference(pfLatOffset);

EditTextPreference pfLonOffset = findPreference("setting_lon_max_offset");
setupDecimalEditTextPreference(pfLonOffset);
}
}
102 changes: 86 additions & 16 deletions app/src/main/java/com/zcshou/gogogo/HistoryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import androidx.appcompat.app.AlertDialog;
import androidx.preference.PreferenceManager;

import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
Expand All @@ -19,11 +20,16 @@
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.view.Gravity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
import java.util.Locale;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -255,6 +261,59 @@ public boolean onQueryTextChange(String newText) {// 当搜索内容改变时触
});
}

private void showDeleteDialog(String locID) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("警告");
builder.setMessage("确定要删除该项历史记录吗?");
builder.setPositiveButton("确定", (dialog, whichButton) -> {
boolean deleteRet = deleteRecord(Integer.parseInt(locID));
if (deleteRet) {
GoUtils.DisplayToast(HistoryActivity.this, getResources().getString(R.string.history_delete_ok));
updateRecordList();
}
});
builder.setNegativeButton("取消", null);

builder.show();
}

private void showInputDialog(String locID, String name) {
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setText(name);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("名称");
builder.setView(input);
builder.setPositiveButton("确认", (dialog, whichButton) -> {
String userInput = input.getText().toString();
DataBaseHistoryLocation.updateHistoryLocation(mHistoryLocationDB, locID, userInput);
updateRecordList();
});
builder.setNegativeButton("取消", null);

builder.show();
}

private String[] randomOffset(String longitude, String latitude) {
String max_offset_default = getResources().getString(R.string.setting_random_offset_default);
double lon_max_offset = Double.parseDouble(Objects.requireNonNull(sharedPreferences.getString("setting_lon_max_offset", max_offset_default)));
double lat_max_offset = Double.parseDouble(Objects.requireNonNull(sharedPreferences.getString("setting_lat_max_offset", max_offset_default)));
double lon = Double.parseDouble(longitude);
double lat = Double.parseDouble(latitude);

double randomLonOffset = (Math.random() * 2 - 1) * lon_max_offset; // Longitude offset (meters)
double randomLatOffset = (Math.random() * 2 - 1) * lat_max_offset; // Latitude offset (meters)

lon += randomLonOffset / 111320; // (meters -> longitude)
lat += randomLatOffset / 110574; // (meters -> latitude)

String offsetMessage = String.format(Locale.US, "经度偏移: %.2f米\n纬度偏移: %.2f米", randomLonOffset, randomLatOffset);
GoUtils.DisplayToast(this, offsetMessage);

return new String[]{String.valueOf(lon), String.valueOf(lat)};
}

private void initRecordListView() {
noRecordText = findViewById(R.id.record_no_textview);
mSearchLayout = findViewById(R.id.search_linear);
Expand All @@ -270,30 +329,41 @@ private void initRecordListView() {
bd09Longitude = latLngStr[0].substring(latLngStr[0].indexOf(':') + 1);
bd09Latitude = latLngStr[1].substring(latLngStr[1].indexOf(':') + 1);

// Random offset
if(sharedPreferences.getBoolean("setting_random_offset", false)) {
String[] offsetResult = randomOffset(bd09Longitude, bd09Latitude);
bd09Longitude = offsetResult[0];
bd09Latitude = offsetResult[1];
}

if (!MainActivity.showLocation(name, bd09Longitude, bd09Latitude)) {
GoUtils.DisplayToast(this, getResources().getString(R.string.history_error_location));
}
this.finish();
});

mRecordListView.setOnItemLongClickListener((parent, view, position, id) -> {
new AlertDialog.Builder(HistoryActivity.this)
.setTitle("警告")//这里是表头的内容
.setMessage("确定要删除该项历史记录吗?")//这里是中间显示的具体信息
.setPositiveButton("确定",
(dialog, which) -> {
String locID = (String) ((TextView) view.findViewById(R.id.LocationID)).getText();
boolean deleteRet = deleteRecord(Integer.parseInt(locID));
PopupMenu popupMenu = new PopupMenu(HistoryActivity.this, view);
popupMenu.setGravity(Gravity.END | Gravity.BOTTOM);
popupMenu.getMenu().add("编辑");
popupMenu.getMenu().add("删除");

popupMenu.setOnMenuItemClickListener(item -> {
String locID = ((TextView) view.findViewById(R.id.LocationID)).getText().toString();
String name = ((TextView) view.findViewById(R.id.LocationText)).getText().toString();
switch (item.getTitle().toString()) {
case "编辑":
showInputDialog(locID, name);
return true;
case "删除":
showDeleteDialog(locID);
return true;
default:
return false;
}
});

if (deleteRet) {
GoUtils.DisplayToast(this, getResources().getString(R.string.history_delete_ok));
updateRecordList();
}
})
.setNegativeButton("取消",
(dialog, which) -> {
})
.show();
popupMenu.show();
return true;
});

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
<string name="setting_bike_default">10.0</string>
<string name="setting_altitude">海拔高度(米)</string>
<string name="setting_altitude_default">55.0</string>
<string name="setting_location">位置</string>
<string name="setting_random_offset">随机偏移</string>
<string name="setting_random_offset_summary">仅在使用历史位置时生效</string>
<string name="setting_random_offset_default">10.0</string>
<string name="setting_lat_max_offset">经度最大偏移距离(米)</string>
<string name="setting_lon_max_offset">纬度最大偏移距离(米)</string>
<string name="setting_group_log">记录</string>
<string name="setting_log_off">关闭日志</string>
<string name="setting_pos_history">历史记录有效期(天)</string>
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/res/xml/preferences_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@

</PreferenceCategory>

<PreferenceCategory
app:title="@string/setting_location"
app:iconSpaceReserved="false">

<SwitchPreferenceCompat
app:key="setting_random_offset"
app:title="@string/setting_random_offset"
app:summary="@string/setting_random_offset_summary"
app:defaultValue="false"
app:iconSpaceReserved="false"/>
<EditTextPreference
app:key="setting_lat_max_offset"
app:title="@string/setting_lat_max_offset"
app:defaultValue="@string/setting_random_offset_default"
app:summary="@string/setting_random_offset_default"
app:iconSpaceReserved="false"/>
<EditTextPreference
app:key="setting_lon_max_offset"
app:title="@string/setting_lon_max_offset"
app:defaultValue="@string/setting_random_offset_default"
app:summary="@string/setting_random_offset_default"
app:iconSpaceReserved="false"/>
</PreferenceCategory>

<PreferenceCategory
app:title="@string/setting_group_log"
app:iconSpaceReserved="false">
Expand Down