Skip to content

Commit 679ef82

Browse files
committed
refactor and fix tox main loop and battery saving
1 parent 051a4a9 commit 679ef82

File tree

6 files changed

+202
-281
lines changed

6 files changed

+202
-281
lines changed

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/HelperGeneric.java

Lines changed: 85 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
import static com.zoffcc.applications.trifa.TrifaToxService.is_tox_started;
169169
import static com.zoffcc.applications.trifa.TrifaToxService.orma;
170170
import static com.zoffcc.applications.trifa.TrifaToxService.stop_tox_fg_done;
171+
import static com.zoffcc.applications.trifa.TrifaToxService.trifa_service_thread;
171172
import static com.zoffcc.applications.trifa.TrifaToxService.vfs;
172173

173174
public class HelperGeneric
@@ -5048,75 +5049,109 @@ static public void append_logger_msg(final String logmsg)
50485049
{
50495050
if (DEBUG_USE_LOGFRIEND)
50505051
{
5051-
// Log.i(TAG, "append_logger_msg:000:LOG_FRIEND_TOXID=" + LOG_FRIEND_TOXID + " msg=" + logmsg);
5052-
String log_friend_pubkey = LOG_FRIEND_TOXID;
5053-
if ((log_friend_pubkey == null) || (log_friend_pubkey.length() < 2))
5052+
try
50545053
{
5055-
try
5054+
// Log.i(TAG, "append_logger_msg:000:LOG_FRIEND_TOXID=" + LOG_FRIEND_TOXID + " msg=" + logmsg);
5055+
String log_friend_pubkey = LOG_FRIEND_TOXID;
5056+
if ((log_friend_pubkey == null) || (log_friend_pubkey.length() < 2))
50565057
{
5057-
log_friend_pubkey = get_g_opts(LOGFRIEND_TOXID_DB_KEY);
5058-
if ((log_friend_pubkey == null) || (log_friend_pubkey.length() < 2))
5058+
try
5059+
{
5060+
log_friend_pubkey = get_g_opts(LOGFRIEND_TOXID_DB_KEY);
5061+
if ((log_friend_pubkey == null) || (log_friend_pubkey.length() < 2))
5062+
{
5063+
// some error with the TOX PUBKEY of log friend
5064+
// Log.i(TAG, "append_logger_msg:ret1: LOG_FRIEND_TOXID=" + log_friend_pubkey);
5065+
return;
5066+
}
5067+
// Log.i(TAG, "append_logger_msg: LOG_FRIEND_TOXID=" + log_friend_pubkey);
5068+
}
5069+
catch (Exception e)
50595070
{
50605071
// some error with the TOX PUBKEY of log friend
5061-
// Log.i(TAG, "append_logger_msg:ret1: LOG_FRIEND_TOXID=" + log_friend_pubkey);
5072+
// Log.i(TAG, "append_logger_msg:ret2: LOG_FRIEND_TOXID=" + LOG_FRIEND_TOXID);
50625073
return;
50635074
}
5064-
// Log.i(TAG, "append_logger_msg: LOG_FRIEND_TOXID=" + log_friend_pubkey);
50655075
}
5066-
catch (Exception e)
5067-
{
5068-
// some error with the TOX PUBKEY of log friend
5069-
// Log.i(TAG, "append_logger_msg:ret2: LOG_FRIEND_TOXID=" + LOG_FRIEND_TOXID);
5070-
return;
5071-
}
5072-
}
5073-
5074-
Log.i(TAG, "append_logger_msg:3:msg=" + logmsg);
50755076

5076-
long pin_timestamp = System.currentTimeMillis();
5077-
Message m = new Message();
5077+
Log.i(TAG, "append_logger_msg:3:msg=" + logmsg);
50785078

5079-
m.is_new = false;
5080-
m.tox_friendpubkey = log_friend_pubkey;
5081-
m.direction = 0; // msg received
5082-
m.TOX_MESSAGE_TYPE = 0;
5083-
m.TRIFA_MESSAGE_TYPE = TRIFA_MSG_TYPE_TEXT.value;
5084-
m.filetransfer_id = -1;
5085-
m.filedb_id = -1;
5086-
m.state = TOX_FILE_CONTROL_CANCEL.value;
5087-
m.ft_accepted = false;
5088-
m.ft_outgoing_started = false;
5089-
m.ft_outgoing_queued = false;
5090-
m.sent_timestamp = pin_timestamp;
5091-
m.sent_timestamp_ms = 0;
5092-
m.rcvd_timestamp = pin_timestamp;
5093-
m.rcvd_timestamp_ms = 0;
5094-
m.text = logmsg;
5095-
m.msg_version = 0;
5096-
m.resend_count = 0;
5097-
m.sent_push = 0;
5098-
m.msg_idv3_hash = "";
5099-
m.msg_id_hash = "";
5100-
m.raw_msgv2_bytes = "";
5079+
long pin_timestamp = System.currentTimeMillis();
5080+
Message m = new Message();
51015081

5102-
long row_id = -99;
5103-
if (MainActivity.message_list_activity != null)
5104-
{
5105-
final long friend_number_ = tox_friend_by_public_key__wrapper(log_friend_pubkey);
5106-
if (MainActivity.message_list_activity.get_current_friendnum() == friend_number_)
5082+
m.is_new = false;
5083+
m.tox_friendpubkey = log_friend_pubkey;
5084+
m.direction = 0; // msg received
5085+
m.TOX_MESSAGE_TYPE = 0;
5086+
m.TRIFA_MESSAGE_TYPE = TRIFA_MSG_TYPE_TEXT.value;
5087+
m.filetransfer_id = -1;
5088+
m.filedb_id = -1;
5089+
m.state = TOX_FILE_CONTROL_CANCEL.value;
5090+
m.ft_accepted = false;
5091+
m.ft_outgoing_started = false;
5092+
m.ft_outgoing_queued = false;
5093+
m.sent_timestamp = pin_timestamp;
5094+
m.sent_timestamp_ms = 0;
5095+
m.rcvd_timestamp = pin_timestamp;
5096+
m.rcvd_timestamp_ms = 0;
5097+
m.text = logmsg;
5098+
m.msg_version = 0;
5099+
m.resend_count = 0;
5100+
m.sent_push = 0;
5101+
m.msg_idv3_hash = "";
5102+
m.msg_id_hash = "";
5103+
m.raw_msgv2_bytes = "";
5104+
5105+
long row_id = -99;
5106+
if (MainActivity.message_list_activity != null)
51075107
{
5108-
row_id = HelperMessage.insert_into_message_db(m, true);
5108+
final long friend_number_ = tox_friend_by_public_key__wrapper(log_friend_pubkey);
5109+
if (MainActivity.message_list_activity.get_current_friendnum() == friend_number_)
5110+
{
5111+
row_id = HelperMessage.insert_into_message_db(m, true);
5112+
}
5113+
else
5114+
{
5115+
row_id = HelperMessage.insert_into_message_db(m, false);
5116+
}
51095117
}
51105118
else
51115119
{
51125120
row_id = HelperMessage.insert_into_message_db(m, false);
51135121
}
5122+
// Log.i(TAG, "append_logger_msg:row_id=" + row_id);
51145123
}
5115-
else
5124+
catch(Exception e)
51165125
{
5117-
row_id = HelperMessage.insert_into_message_db(m, false);
51185126
}
5119-
// Log.i(TAG, "append_logger_msg:row_id=" + row_id);
5127+
}
5128+
}
5129+
5130+
static public void trigger_proper_wakeup_outside_tox_service_thread()
5131+
{
5132+
append_logger_msg(TAG + "::trigger_proper_wakeup_outside_tox_service_thread");
5133+
TrifaToxService.need_wakeup_now = true;
5134+
global_last_activity_for_battery_savings_ts = System.currentTimeMillis();
5135+
try
5136+
{
5137+
trifa_service_thread.interrupt();
5138+
}
5139+
catch(Exception ignored)
5140+
{
5141+
}
5142+
}
5143+
5144+
static public void trigger_proper_wakeup_from_tox_service_thread()
5145+
{
5146+
append_logger_msg(TAG + "::trigger_proper_wakeup_from_tox_service_thread");
5147+
TrifaToxService.need_wakeup_now = true;
5148+
global_last_activity_for_battery_savings_ts = System.currentTimeMillis();
5149+
try
5150+
{
5151+
trifa_service_thread.interrupt();
5152+
}
5153+
catch(Exception ignored)
5154+
{
51205155
}
51215156
}
51225157
}

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/MyExternReceiver.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import androidx.core.app.NotificationCompat;
3535

3636
import static com.zoffcc.applications.trifa.HelperGeneric.append_logger_msg;
37+
import static com.zoffcc.applications.trifa.HelperGeneric.trigger_proper_wakeup_outside_tox_service_thread;
3738
import static com.zoffcc.applications.trifa.TRIFAGlobals.global_last_activity_for_battery_savings_ts;
3839
import static com.zoffcc.applications.trifa.TrifaToxService.trifa_service_thread;
3940

@@ -163,9 +164,7 @@ public void run()
163164
if (trifa_service_thread != null)
164165
{
165166
append_logger_msg(TAG + "::" + "need_wakeup_now trigger 002");
166-
TrifaToxService.need_wakeup_now = true;
167-
global_last_activity_for_battery_savings_ts = System.currentTimeMillis();
168-
trifa_service_thread.interrupt();
167+
trigger_proper_wakeup_outside_tox_service_thread();
169168
}
170169
}
171170
catch (Exception e)

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/MyTokenReceiver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import androidx.core.app.NotificationCompat;
3636

3737
import static com.zoffcc.applications.trifa.HelperGeneric.set_g_opts;
38+
import static com.zoffcc.applications.trifa.HelperGeneric.trigger_proper_wakeup_outside_tox_service_thread;
3839
import static com.zoffcc.applications.trifa.TRIFAGlobals.NOTIFICATION_NTFY_PUSH_URL_PREFIX;
3940
import static com.zoffcc.applications.trifa.TRIFAGlobals.NOTIFICATION_TOKEN_DB_KEY_NEED_ACK;
4041
import static com.zoffcc.applications.trifa.TrifaToxService.trifa_service_thread;
@@ -201,8 +202,7 @@ public void run()
201202

202203
if (trifa_service_thread != null)
203204
{
204-
TrifaToxService.need_wakeup_now = true;
205-
trifa_service_thread.interrupt();
205+
trigger_proper_wakeup_outside_tox_service_thread();
206206
}
207207
}
208208
catch (Exception e)

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/TRIFAGlobals.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public class TRIFAGlobals
4040
static long global_self_last_went_online_timestamp = -1;
4141
static long global_self_last_went_offline_timestamp = -1;
4242
static long global_last_activity_for_battery_savings_ts = -1;
43-
static long global_self_last_entered_battery_saving_timestamp = -1;
4443
static boolean global_showing_messageview = false;
4544
static boolean global_showing_anygroupview = false;
4645
final static int TOX_MIN_NORMAL_ITERATE_DELTA_MS = 100;
@@ -67,9 +66,6 @@ public class TRIFAGlobals
6766
1000 * 60 * 2; // bootstrap again after 2 minutes offline
6867
final static int SECONDS_TO_STAY_ONLINE_IN_BATTERY_SAVINGS_MODE = 60 * 3; // 3 minutes
6968
static long BATTERY_OPTIMIZATION_SLEEP_IN_MILLIS = 15 * 1000 * 60; // 15 minutes default
70-
static int BATTERY_OPTIMIZATION_LAST_SLEEP1 = -1;
71-
static int BATTERY_OPTIMIZATION_LAST_SLEEP2 = -1;
72-
static int BATTERY_OPTIMIZATION_LAST_SLEEP3 = -1;
7369

7470
static int AUTO_ACCEPT_FT_MAX_IMAGE_SIZE_IN_MB = 12;
7571
static int AUTO_ACCEPT_FT_MAX_VIDEO_SIZE_IN_MB = 40;

0 commit comments

Comments
 (0)