wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*  Copyright (C) 2022-2024 Arjan Schrijver, Daniel Dakhno, José Rebelo
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge 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 Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities;
 
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.annotation.NonNull;
 
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
 
import java.util.TimeZone;
 
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.entities.WorldClock;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
 
public class WorldClockDetails extends AbstractGBActivity {
    private WorldClock worldClock;
    private GBDevice device;
 
    ArrayAdapter<String> timezoneAdapter;
 
    TextView worldClockTimezone;
    EditText worldClockLabel;
    EditText worldClockCode;
    View worldClockEnabledCard;
    CheckBox worldClockEnabled;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_world_clock_details);
 
        worldClock = (WorldClock) getIntent().getSerializableExtra(WorldClock.EXTRA_WORLD_CLOCK);
 
        if (worldClock == null) {
            GB.toast("No worldClock provided to WorldClockDetails Activity", Toast.LENGTH_LONG, GB.ERROR);
            finish();
            return;
        }
 
        worldClockEnabledCard = findViewById(R.id.card_enabled);
        worldClockEnabled = findViewById(R.id.world_clock_enabled);
        worldClockTimezone = findViewById(R.id.world_clock_timezone);
        worldClockLabel = findViewById(R.id.world_clock_label);
        worldClockCode = findViewById(R.id.world_clock_code);
 
        device = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
        if (device == null) {
            GB.toast("No device provided to WorldClockDetails Activity", Toast.LENGTH_LONG, GB.ERROR);
            finish();
            return;
        }
 
        final DeviceCoordinator coordinator = device.getDeviceCoordinator();
 
        final String[] timezoneIDs = TimeZone.getAvailableIDs();
        timezoneAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, timezoneIDs);
 
        final View cardTimezone = findViewById(R.id.card_timezone);
        cardTimezone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new MaterialAlertDialogBuilder(WorldClockDetails.this).setAdapter(timezoneAdapter, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        worldClock.setTimeZoneId(timezoneIDs[i]);
                        updateUiFromWorldClock();
                    }
                }).create().show();
            }
        });
 
        if (coordinator.supportsDisabledWorldClocks()) {
            worldClockEnabled.setOnCheckedChangeListener((buttonView, isChecked) -> {
                worldClock.setEnabled(isChecked);
            });
        } else {
            worldClockEnabledCard.setVisibility(View.GONE);
        }
 
        worldClockLabel.setFilters(new InputFilter[]{new InputFilter.LengthFilter(coordinator.getWorldClocksLabelLength())});
        worldClockLabel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(final CharSequence s, int start, int count, int after) {
            }
 
            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
            }
 
            @Override
            public void afterTextChanged(final Editable s) {
                worldClock.setLabel(s.toString());
            }
        });
 
        worldClockCode.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
        worldClockCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(final CharSequence s, int start, int count, int after) {
            }
 
            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
            }
 
            @Override
            public void afterTextChanged(final Editable s) {
                worldClock.setCode(s.toString());
            }
        });
 
        final FloatingActionButton fab = findViewById(R.id.fab_save);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateWorldClock();
                WorldClockDetails.this.setResult(1);
                finish();
            }
        });
 
        updateUiFromWorldClock();
    }
 
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        final int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            // back button
            // TODO confirm when exiting without saving
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
 
    private void updateWorldClock() {
        DBHelper.store(worldClock);
    }
 
    @Override
    protected void onSaveInstanceState(@NonNull final Bundle state) {
        super.onSaveInstanceState(state);
        state.putSerializable("worldClock", worldClock);
    }
 
    @Override
    protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        worldClock = (WorldClock) savedInstanceState.getSerializable("worldClock");
        updateUiFromWorldClock();
    }
 
    public void updateUiFromWorldClock() {
        final DeviceCoordinator coordinator = device.getDeviceCoordinator();
        final int maxLabelLength = coordinator.getWorldClocksLabelLength();
 
        worldClockEnabled.setChecked(worldClock.getEnabled() == null || worldClock.getEnabled());
 
        final String oldTimezone = worldClockTimezone.getText().toString();
 
        worldClockTimezone.setText(worldClock.getTimeZoneId());
 
        // Check if the label was still the default (the timezone city name)
        // If so, and if the user changed the timezone, update the label to match the new city name
        if (!StringUtils.isNullOrEmpty(oldTimezone) && !oldTimezone.equals(worldClock.getTimeZoneId())) {
            final String[] oldTimezoneParts = oldTimezone.split("/");
            final String[] newTimezoneParts = worldClock.getTimeZoneId().split("/");
            final String newLabel = StringUtils.truncate(newTimezoneParts[newTimezoneParts.length - 1], maxLabelLength);
            final String oldLabel = StringUtils.truncate(oldTimezoneParts[oldTimezoneParts.length - 1], maxLabelLength);
            final String userLabel = worldClockLabel.getText().toString();
            if (StringUtils.isNullOrEmpty(userLabel) || userLabel.equals(oldLabel)) {
                // The label was still the original, so let's override it with the new city
                worldClock.setLabel(newLabel);
            }
            final String newCode = StringUtils.truncate(newLabel, 3).toUpperCase();
            final String oldCode = StringUtils.truncate(oldLabel, 3).toUpperCase();
            final String userCode = worldClockCode.getText().toString();
            if (StringUtils.isNullOrEmpty(userCode) || userCode.equals(oldCode)) {
                // The code was still the original, so let's override it with the new one
                worldClock.setCode(newCode);
            }
        }
 
        worldClockLabel.setText(worldClock.getLabel());
        worldClockCode.setText(worldClock.getCode());
    }
}