Skip to content

Commit

Permalink
Merge pull request #27 from FazziCLAY/feature/pin-code
Browse files Browse the repository at this point in the history
Feature/pin code
  • Loading branch information
FazziCLAY authored Mar 8, 2023
2 parents 9ff1b36 + 3dc1db1 commit 7126edc
Show file tree
Hide file tree
Showing 43 changed files with 1,147 additions and 487 deletions.
7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
// =====
var isDev = false
var verBuild = 106
var verName = "1.0.2"
var verBuild = 108
var verName = "1.0.3"
// =====

applicationId "com.fazziclay.opentoday" + (isDev ? ".dev" : "")
Expand Down
1 change: 1 addition & 0 deletions app/src/main/assets/CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OwO
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.fazziclay.opentoday.app;

public class ActivitySettings {
private boolean clockVisible = true;
private boolean notificationsVisible = true;


public ActivitySettings setClockVisible(boolean clockVisible) {
this.clockVisible = clockVisible;
return this;
}

public ActivitySettings invertClockVisible() {
this.clockVisible = !this.clockVisible;
return this;
}

public ActivitySettings setNotificationsVisible(boolean notificationsVisible) {
this.notificationsVisible = notificationsVisible;
return this;
}

public ActivitySettings invertNotificationsVisible() {
this.notificationsVisible = !this.notificationsVisible;
return this;
}

public boolean isClockVisible() {
return clockVisible;
}

public boolean isNotificationsVisible() {
return notificationsVisible;
}
}
31 changes: 27 additions & 4 deletions app/src/main/java/com/fazziclay/opentoday/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fazziclay.opentoday.app.datafixer.DataFixer;
import com.fazziclay.opentoday.app.datafixer.FixResult;
import com.fazziclay.opentoday.app.items.ItemManager;
import com.fazziclay.opentoday.app.pincode.PinCodeManager;
import com.fazziclay.opentoday.app.receiver.QuickNoteReceiver;
import com.fazziclay.opentoday.app.settings.SettingsManager;
import com.fazziclay.opentoday.gui.activity.CrashReportActivity;
Expand Down Expand Up @@ -54,6 +55,8 @@ public class App extends Application {
// Shared preference
public static final String SHARED_NAME = "main";
public static final String SHARED_KEY_IS_SETUP_DONE = "isSetupDone";
public static final String SHARED_KEY_PINCODE = "app_pinCode";
public static final String SHARED_KEY_LAST_TAB = "app_tabInclude_lastTabId";

// DEBUG
public static final boolean SHADOW_RELEASE = false;
Expand Down Expand Up @@ -85,15 +88,16 @@ public static App get() {
@AppInitIfNeed private SettingsManager settingsManager = null;
@AppInitIfNeed private ColorHistoryManager colorHistoryManager = null;
@AppInitIfNeed private Telemetry telemetry = null;
private PinCodeManager pinCodeManager = null;
@AppInitIfNeed private License[] openSourceLicenses = null;
private final List<FeatureFlag> featureFlags = new ArrayList<>(App.DEBUG ? Arrays.asList(
FeatureFlag.ITEM_DEBUG_TICK_COUNTER,
FeatureFlag.ITEM_EDITOR_SHOW_COPY_ID_BUTTON,
//FeatureFlag.ITEM_EDITOR_SHOW_COPY_ID_BUTTON,
FeatureFlag.AVAILABLE_LOGS_OVERLAY,
FeatureFlag.NONE,
FeatureFlag.SHOW_APP_STARTUP_TIME_IN_PREMAIN_ACTIVITY,
FeatureFlag.ALWAYS_SHOW_SAVE_STATUS,
FeatureFlag.SHOW_MAINACTIVITY_STARTUP_TIME,
//FeatureFlag.SHOW_APP_STARTUP_TIME_IN_PREMAIN_ACTIVITY,
//FeatureFlag.ALWAYS_SHOW_SAVE_STATUS,
//FeatureFlag.SHOW_MAINACTIVITY_STARTUP_TIME,
FeatureFlag.AVAILABLE_UI_PERSONAL_TICK,
FeatureFlag.AVAILABLE_RESTART_ACTIVITY,
FeatureFlag.AVAILABLE_RESET_SETUP
Expand All @@ -114,6 +118,8 @@ public void onCreate() {

registryNotificationsChannels();

this.pinCodeManager = new PinCodeManager(this);

if (fixResult.isVersionFileUpdateRequired()) updateVersionFile();
if (fixResult.isFixed()) {
getTelemetry().send(new Telemetry.DataFixerLogsLPacket(fixResult.getDataVersion(), fixResult.getLogs()));
Expand All @@ -124,6 +130,18 @@ public void onCreate() {
this.appStartupTime = System.currentTimeMillis() - start;
}

public boolean isPinCodeNeed() {
return this.pinCodeManager.isPinCodeSet();
}

public boolean isPinCodeAllow(String p) {
return p.equals(this.pinCodeManager.getPinCode());
}

public int getPinCodeLength() {
return this.pinCodeManager.getPinCode().length();
}

private void registryNotificationsChannels() {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(new NotificationChannel(NOTIFICATION_QUCIKNOTE_CHANNEL, getString(R.string.notification_quickNote_title), NotificationManager.IMPORTANCE_HIGH));
Expand Down Expand Up @@ -324,6 +342,11 @@ public License[] getOpenSourcesLicenses() {
preCheckOpenSourceLicenses();
return this.openSourceLicenses;
}

public PinCodeManager getPinCodeManager() {
return pinCodeManager;
}

public boolean isAppInForeground() { return appInForeground; }
public void setAppInForeground(boolean appInForeground) { this.appInForeground = appInForeground; }
public List<FeatureFlag> getFeatureFlags() {return featureFlags;}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.os.Build;

import com.fazziclay.opentoday.util.L;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -225,7 +223,7 @@ public String convertToText() {

String loggerLLogs;
try {
loggerLLogs = L.getInstance().getFinalText();
loggerLLogs = "L logger deleted from this version.";
} catch (Exception e) {
loggerLLogs = "(Unknown " + e + ")";
}
Expand Down
32 changes: 17 additions & 15 deletions app/src/main/java/com/fazziclay/opentoday/app/Telemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.fazziclay.neosocket.PacketHandler;
import com.fazziclay.neosocket.packet.Packet;
import com.fazziclay.neosocket.packet.PacketsRegistry;
import com.fazziclay.opentoday.util.L;
import com.fazziclay.opentoday.util.Logger;
import com.fazziclay.opentoday.util.NetworkUtil;

import org.json.JSONException;
Expand All @@ -28,6 +28,7 @@
import ru.fazziclay.opentoday.telemetry.packet.PacketSetVersion;

public class Telemetry {
private static final String TAG = "Telemetry";
public static final PacketsRegistry REGISTRY = new TelemetryPackets();
private static final boolean NO_DELAY = (App.DEBUG && false);
private static final String URL = "https://fazziclay.github.io/api/project_3/v2/telemetry_v1.json";
Expand Down Expand Up @@ -64,11 +65,11 @@ public void queryTelemetryStatus() {

public void send(LPacket lPacket) {
if (!isEnabled) return;
L.o("Telemetry send");
Logger.d(TAG, "send()");
if (lPacket.isDelay() && !NO_DELAY) {
long last = getLastSend(lPacket.getClass().getName());
long curr = System.currentTimeMillis();
L.o("Telemetry last=", last, "curr=", curr);
Logger.d(TAG, "send() last=", last, "curr=", curr);
boolean hoursNoDelayed = curr - last < 24*60*60*1000;
if (hoursNoDelayed) {
GregorianCalendar g = new GregorianCalendar();
Expand All @@ -77,7 +78,7 @@ public void send(LPacket lPacket) {
g = new GregorianCalendar();
g.setTimeInMillis(curr);
int d2 = g.get(Calendar.DAY_OF_MONTH);
L.o("Telemetry d1=", d1, "d2=", d2);
Logger.d(TAG, "send() d1=", d1, "d2=", d2);
if (d1 == d2) {
return;
}
Expand All @@ -86,11 +87,11 @@ public void send(LPacket lPacket) {

SendThread thread = new SendThread(lPacket.getPacket());
thread.start();
L.o("Telemetry send: wait");
Logger.d(TAG, "send(): wait");
while (thread.isBusy() && lPacket.isBlocking()) {
thread.tick();
}
L.o("Telemetry send: wait: done");
Logger.d(TAG, "send(): wait: done");
setLastSend(lPacket.getClass().getName());
}

Expand Down Expand Up @@ -136,7 +137,7 @@ public SendThread(Packet packet) {
public void run() {
isBusy = true;
try {
L.o("Telemetry SaveThread: ");
Logger.d(TAG, "SaveThread run()");
if (!isTelemetryStatusQuerying && telemetryStatus == null) {
queryTelemetryStatus();
}
Expand All @@ -145,20 +146,21 @@ public void run() {
tick();
}
}
L.o("Telemetry SaveThread: query done");
Logger.d(TAG, "SaveThread: query done");
if (telemetryStatus == null) {
throw new RuntimeException("WTF: don't query telemetryStatus");
}
if (telemetryStatus.isEnabled()) {
L.o("Telemetry SaveThread: enabled! client new");
Logger.d(TAG, "SaveThread: enabled! client new");
Client client = new Client(telemetryStatus.getHost(), telemetryStatus.getPort(), REGISTRY, new PacketHandler() {
@Override
public void received(Client client, Packet packet) {
L.o("Telemetry", "Received: ", packet.toString());
Logger.d(TAG, "[client] received: ", packet.toString());
}

@Override
public void setup(Client client) {
Logger.d(TAG, "[client] setup");
try {
client.send(new PacketSetVersion(2));
client.send(new Packet20004Login(app.getInstanceId()));
Expand All @@ -169,18 +171,18 @@ public void setup(Client client) {
send = true;

} catch (Exception e) {
e.printStackTrace();
Logger.e(TAG, "[client] setup exception", e);
}
}

@Override
public void preDisconnect(Client client) {
L.o("Telemetry", "preDisconnect");
Logger.d(TAG, "[client] preDisconnect");
}

@Override
public void fatalException(Client client, Exception e) {
L.o("Telemetry", "fatalException", e);
Logger.d(TAG, "[client] fatalException", e);
}
});
new Thread(() -> {
Expand All @@ -191,12 +193,12 @@ public void fatalException(Client client, Exception e) {
}
}).start();

L.o("Telemetry wait send or 10 ser");
Logger.d(TAG, "wait send or 10 ser");
long start = System.currentTimeMillis();
while (!send && System.currentTimeMillis() - start < 10 * 1000) {
tick();
}
L.o("Telemetry wait send or 10 ser: done");
Logger.d(TAG, "wait send or 10 ser: done");

}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.fazziclay.opentoday.app.pincode;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;

import com.fazziclay.javaneoutil.FileUtil;
import com.fazziclay.opentoday.app.App;

import java.io.File;

public class PinCodeManager {
private final SharedPreferences sharedPreferences;
private final File backupFile;


public PinCodeManager(Context context) {
this.sharedPreferences = context.getSharedPreferences(App.SHARED_NAME, MODE_PRIVATE);
this.backupFile = new File(context.getExternalFilesDir(""), "pcb");
}

public boolean isPinCodeSet() {
return sharedPreferences.contains(App.SHARED_KEY_PINCODE);
}

public String getPinCode() {
return sharedPreferences.getString(App.SHARED_KEY_PINCODE, "0000");
}

public void disablePinCode() {
sharedPreferences.edit().remove(App.SHARED_KEY_PINCODE).apply();
if (FileUtil.isExist(backupFile)) {
FileUtil.delete(backupFile);
}
}

public void enablePinCode(String pin) {
if (pin.isEmpty()) {
throw new ContainNonDigitChars();
}
for (char c : pin.toCharArray()) {
if (!Character.isDigit(c)) {
throw new ContainNonDigitChars();
}
}
sharedPreferences.edit().putString(App.SHARED_KEY_PINCODE, pin).apply();
FileUtil.setText(backupFile, pin);
}

public static class ContainNonDigitChars extends RuntimeException {
public ContainNonDigitChars() {
super("Contains non-digit chars in pin-code!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.fazziclay.opentoday.app.App;
import com.fazziclay.opentoday.app.TickSession;
import com.fazziclay.opentoday.app.items.ItemManager;
import com.fazziclay.opentoday.util.L;
import com.fazziclay.opentoday.util.Logger;

import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -32,7 +32,7 @@ public void onReceive(Context context, Intent intent) {

if (intent != null && intent.getExtras() != null && intent.getExtras().containsKey("debugMessage")) {
String s = intent.getExtras().getString("debugMessage", "none");
L.o("ItemsTickReceiver", "DebugMessage! " + s);
Logger.d("ItemsTickReceiver", "DebugMessage! " + s);
}

debugNotification(context);
Expand Down
Loading

0 comments on commit 7126edc

Please sign in to comment.