package Ġnet . os mand . telegram . helpers Ċ Ċ import Ġandroid . os . Handler Ċ import Ġandroid . os . Message Ċ import Ġnet . os mand . Location Ċ import Ġnet . os mand . Platform Util Ċ import Ġnet . os mand . telegram .* Ċ import Ġnet . os mand . telegram . helpers . Location Messages . Buffer Message Ċ import Ġnet . os mand . telegram . notifications . Telegram Notification . Notification Type Ċ import Ġnet . os mand . telegram . utils . Android Network Utils Ċ import Ġnet . os mand . telegram . utils . BASE _ URL Ċ import Ġnet . os mand . telegram . utils . Os mand Location Utils Ċ import Ġnet . os mand . util . Map Utils Ċ import Ġorg . dr ink less . td . lib core . telegram . Td Api Ċ import Ġorg . json . JSONException Ċ import Ġorg . json . JSONObject Ċ Ċ private Ġconst Ġval ĠUSER _ SET _ LI VE _ PERIOD _ DELAY _ MS Ġ= Ġ 5 0 0 0 Ġ// Ġ 5 Ġsec Ċ Ċ private Ġconst Ġval ĠMIN _ MESSAGE _ SEND ING _ INTERVAL _ MS Ġ= Ġ 1 0 0 0 Ġ// Ġ 1 Ġsec Ċ private Ġconst Ġval ĠMIN _ MESSAGES _ BUFFER _ CHECK _ INTERVAL _ MS Ġ= Ġ 3 0 0 Ġ// Ġ 0 . 3 Ġsec Ċ Ċ private Ġconst Ġval ĠS END _ MESSAGES _ BUFFER _ MS _ MSG _ ID Ġ= Ġ 5 0 0 0 Ċ Ċ class ĠShare Location Helper ( private Ġval Ġapp : ĠTelegram Application ) Ġ{ ĊĊ ĉ private Ġval Ġlog Ġ= ĠPlatform Util . getLog ( Share Location Helper :: class . java ) ĊĊ ĉ var Ġsharing Location Ġ= Ġfalse Ċĉ ĉ private Ġset ĊĊ ĉ var Ġduration Ġ= Ġ 0 L Ċĉ ĉ private Ġset ĊĊ ĉ var Ġdistance Ġ= Ġ 0 Ċĉ ĉ private Ġset ĊĊ ĉ var Ġlast Location UpdateTime Ġ= Ġ 0 L ĊĊ ĉ private Ġvar Ġlast Share Location Message Time Ġ= Ġ 0 L ĊĊ ĉ var Ġlast Location : ĠLocation ? Ġ= Ġnull Ċĉ ĉ set ( value ) Ġ{ Ċĉĉ ĉ if Ġ( last TimeIn Millis Ġ== Ġ 0 L ) Ġ{ Ċĉĉĉ ĉ last TimeIn Millis Ġ= ĠSystem . currentTimeMillis () Ċĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉ ĉ val ĠcurrentTime In Millis Ġ= ĠSystem . currentTimeMillis () Ċĉĉĉ ĉ duration Ġ+= ĠcurrentTime In Millis Ġ- Ġlast TimeIn Millis Ċĉĉĉ ĉ last TimeIn Millis Ġ= ĠcurrentTime In Millis Ċĉĉ ĉ } Ċĉĉ ĉ if Ġ( last Location Ġ!= Ġnull Ġ&& Ġvalue Ġ!= Ġnull ) Ġ{ Ċĉĉĉ ĉ distance Ġ+= Ġvalue . distance To ( last Location ). toInt () Ċĉĉ ĉ } Ċĉĉ ĉ field Ġ= Ġvalue Ċĉ ĉ } ĊĊ ĉ private Ġvar Ġlast TimeIn Millis : ĠLong Ġ= Ġ 0 L ĊĊ ĉ private Ġvar Ġsend Buffer Messages Handler Ġ= ĠHandler () Ċ ĉ private Ġvar Ġsend Buffer Messages Runnable Ġ= ĠRunnable Ġ{ Ġapp . share Location Helper . check And Send Buffer Messages ( false ) Ġ} ĊĊ ĉ fun Ġupdate Location ( location : ĠLocation ?) Ġ{ Ċĉ ĉ val Ġlast Point Ġ= Ġlast Location Ċĉ ĉ var Ġrecord Ġ= Ġtrue Ċĉ ĉ if Ġ( location Ġ!= Ġnull ) Ġ{ Ċĉĉ ĉ val Ġmin Distance Ġ= Ġapp . settings . min Location Distance Ċĉĉ ĉ if Ġ( min Distance Ġ> Ġ 0 Ġ&& Ġlast Point Ġ!= Ġnull ) Ġ{ Ċĉĉĉ ĉ val Ġcalculated Distance Ġ= ĠMap Utils . get Distance ( last Point . latitude , Ġlast Point . longitude , Ġlocation . latitude , Ġlocation . longitude ) Ċĉĉĉ ĉ if Ġ( calculated Distance Ġ< Ġmin Distance ) Ġ{ Ċĉĉĉĉ ĉ record Ġ= Ġfalse Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉĉ ĉ val Ġaccuracy Ġ= Ġapp . settings . min Location Accuracy Ċĉĉ ĉ if Ġ( accuracy Ġ> Ġ 0 Ġ&& Ġ(! location . has Accuracy () Ġ|| Ġlocation . accuracy Ġ> Ġaccuracy )) Ġ{ Ċĉĉĉ ĉ record Ġ= Ġfalse Ċĉĉ ĉ } Ċĉĉ ĉ val Ġmin Speed Ġ= Ġapp . settings . min Location Speed Ċĉĉ ĉ if Ġ( min Speed Ġ> Ġ 0 Ġ&& Ġ(! location . has Speed () Ġ|| Ġlocation . speed Ġ< Ġmin Speed )) Ġ{ Ċĉĉĉ ĉ record Ġ= Ġfalse Ċĉĉ ĉ } ĊĊĉĉ ĉ if Ġ( record ) Ġ{ Ċĉĉĉ ĉ last Location UpdateTime Ġ= ĠSystem . currentTimeMillis () Ċĉĉĉ ĉ last Location Ġ= Ġlocation Ċĉĉĉ ĉ if Ġ( app . settings . get Ch ats Share Info (). isNotEmpty ()) Ġ{ Ċĉĉĉĉ ĉ app . show Location Helper . should B link Widget Ġ= Ġtrue Ċĉĉĉĉ ĉ share Location Messages ( location , Ġapp . telegram Helper . getCurrent UserId ()) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ċĉ ĉ app . settings . update Sharing Status History () Ċĉ ĉ refresh Notification () Ċ ĉ } ĊĊ ĉ fun Ġupdate Send Live Messages () Ġ{ Ċĉ ĉ log . info (" update Send Live Messages ") Ċĉ ĉ if Ġ( app . settings . has Any Chat To Share Location ()) Ġ{ Ċĉĉ ĉ app . settings . get Ch ats Share Info (). forEach Ġ{ Ġ( chat Id , Ġshare Info ) Ġ-> Ċĉĉĉ ĉ val ĠcurrentTime Ġ= ĠSystem . currentTimeMillis () Ġ/ Ġ 1 0 0 0 Ċĉĉĉ ĉ when Ġ{ Ċĉĉĉĉ ĉ share Info . get Chat Live Message Expire Time () Ġ<= Ġ 0 Ġ-> Ġ{ Ċĉĉĉĉĉ ĉ app . settings . share Location To Chat ( chat Id , Ġfalse ) Ċĉĉĉĉĉ ĉ app . settings . add Time Period To Last Item ( share Info . chat Id , Ġshare Info . live Period ) Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ currentTime Ġ> Ġshare Info . current Message Limit Ġ-> Ġ{ Ċĉĉĉĉĉ ĉ share Info . apply Ġ{ Ċĉĉĉĉĉĉ ĉ val Ġnew Live Period Ġ= Ċĉĉĉĉĉĉĉ ĉ if Ġ( live Period Ġ> ĠTelegram Helper . MAX _ LOCATION _ MESSAGE _ LI VE _ PERIOD _ SEC ) Ġ{ Ċĉĉĉĉĉĉĉĉ ĉ live Period Ġ- ĠTelegram Helper . MAX _ LOCATION _ MESSAGE _ LI VE _ PERIOD _ SEC Ċĉĉĉĉĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉĉĉĉĉ ĉ live Period Ċĉĉĉĉĉĉĉ ĉ } Ċĉĉĉĉĉĉ ĉ live Period Ġ= Ġnew Live Period Ċĉĉĉĉĉĉ ĉ current Message Limit Ġ= ĠcurrentTime Ġ+ ĠMath . min ( new Live Period , ĠTelegram Helper . MAX _ LOCATION _ MESSAGE _ LI VE _ PERIOD _ SEC . to Long ()) Ċĉĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ share Info . user Set Live Period Ġ!= Ġshare Info . live Period Ċĉĉĉĉĉĉ ĉ && Ġ( share Info . user Set Live Period Start Ġ+ ĠUSER _ SET _ LI VE _ PERIOD _ DELAY _ MS ) Ġ> ĠcurrentTime Ġ-> Ġ{ Ċĉĉĉĉĉ ĉ share Info . apply Ġ{ Ċĉĉĉĉĉĉ ĉ live Period Ġ= Ġshare Info . user Set Live Period Ċĉĉĉĉĉĉ ĉ current Message Limit Ġ= ĠcurrentTime Ġ+ ĠMath . min ( live Period , ĠTelegram Helper . MAX _ LOCATION _ MESSAGE _ LI VE _ PERIOD _ SEC . to Long ()) Ċĉĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ } Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ġelse Ġ{ Ċĉĉ ĉ stop Sharing Location () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġcheck And Send Buffer Messages ( check Network Type Allowed : ĠBoolean Ġ= Ġtrue ) Ġ{ Ċĉ ĉ log . debug (" check And Send Buffer Messages ") Ċĉ ĉ var Ġpending Messages Limit Reached Ġ= Ġfalse Ċĉ ĉ app . settings . get Ch ats Share Info (). forEach Ġ{ Ġ( chat Id , Ġshare Info ) Ġ-> Ċĉĉ ĉ check And Send Buffer Messages To Chat ( chat Id , Ġtrue ) Ċĉĉ ĉ if Ġ( share Info . is Pending Messages Limit Reached ()) Ġ{ Ċĉĉĉ ĉ pending Messages Limit Reached Ġ= Ġtrue Ċĉĉ ĉ } Ċĉ ĉ } Ċĉ ĉ if Ġ( pending Messages Limit Reached Ġ&& Ġcheck Network Type Allowed ) Ġ{ Ċĉĉ ĉ update Network Type () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġcheck And Send Buffer Messages Delayed () Ġ{ Ċĉ ĉ if Ġ(! send Buffer Messages Handler . has Messages ( SEND _ MESSAGES _ BUFFER _ MS _ MSG _ ID )) Ġ{ Ċĉĉ ĉ val Ġmsg Ġ= ĠMessage . obtain ( send Buffer Messages Handler , Ġsend Buffer Messages Runnable ) Ċĉĉ ĉ msg . what Ġ= ĠS END _ MESSAGES _ BUFFER _ MS _ MSG _ ID Ċĉĉ ĉ send Buffer Messages Handler . sendMessage Delayed ( msg , ĠMIN _ MESSAGES _ BUFFER _ CHECK _ INTERVAL _ MS . to Long () Ġ+ Ġ 1 L ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġcheck And Send Buffer Messages To Chat ( chat Id : ĠLong , Ġforced : ĠBoolean Ġ= Ġfalse ): ĠInt Ġ{ Ċĉ ĉ val Ġshare Ch ats Info Ġ= Ġapp . settings . get Ch ats Share Info () Ċĉ ĉ val Ġshare Info Ġ= Ġshare Ch ats Info [ chat Id ] Ċĉ ĉ if Ġ( share Info Ġ!= Ġnull ) Ġ{ Ċĉĉ ĉ val Ġbuffered Messages Count Ġ= Ġapp . location Messages . get Buffered Messages Count For Chat ( chat Id ) Ċĉĉ ĉ if Ġ( buffered Messages Count Ġ> Ġ 0 ) Ġ{ Ċĉĉĉ ĉ check And Send Buffer Messages Delayed () Ċĉĉ ĉ } Ċĉĉ ĉ val ĠcurrentTime Ġ= ĠSystem . currentTimeMillis () Ċĉĉ ĉ if Ġ( currentTime Ġ- Ġshare Info . last Buffer Check Time Ġ< ĠMIN _ MESSAGES _ BUFFER _ CHECK _ INTERVAL _ MS Ġ&& Ġ! forced ) Ġ{ Ċĉĉĉ ĉ return Ġbuffered Messages Count Ċĉĉ ĉ } Ċĉĉ ĉ share Info . last Buffer Check Time Ġ= ĠcurrentTime ĊĊĉĉ ĉ app . location Messages . get Buffered Text Messages For Chat ( chat Id ). take ( MAX _ MESSAGES _ IN _ TD LIB _ PER _ CHAT ). forEach Ġ{ Ċĉĉĉ ĉ if Ġ(! share Info . is Pending Text Messages Limit Reached ()) Ġ{ Ċĉĉĉĉ ĉ if Ġ( it . device Name . isEmpty ()) Ġ{ Ċĉĉĉĉĉ ĉ if Ġ(! share Info . pending Text Message Ġ&& Ġshare Info . is Text MessageId Present ()) Ġ{ Ċĉĉĉĉĉĉ ĉ val Ġcontent Ġ= ĠOs mand Location Utils . getText Message Content ( share Info . update Text MessageId , Ġit , Ġapp ) Ċĉĉĉĉĉĉ ĉ app . telegram Helper . edit Text Location ( share Info , Ġcontent ) Ċĉĉĉĉĉĉ ĉ app . location Messages . remove Buffered Message ( it ) Ċĉĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉĉ ĉ send Location To Bot ( it , Ġshare Info , ĠSHARE _ TYPE _ TEXT ) Ċĉĉĉĉ ĉ } Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉĉ ĉ app . location Messages . get Buffered Map Messages For Chat ( chat Id ). take ( MAX _ MESSAGES _ IN _ TD LIB _ PER _ CHAT ). forEach Ġ{ Ċĉĉĉ ĉ if Ġ(! share Info . is Pending Map Messages Limit Reached ()) Ġ{ Ċĉĉĉĉ ĉ if Ġ( it . device Name . isEmpty ()) Ġ{ Ċĉĉĉĉĉ ĉ if Ġ(! share Info . pending Map Message ) Ġ{ Ċĉĉĉĉĉĉ ĉ if Ġ( share Info . is Map MessageId Present ()) Ġ{ Ċĉĉĉĉĉĉĉ ĉ app . telegram Helper . edit Map Location ( share Info , Ġit ) Ċĉĉĉĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉĉĉĉ ĉ app . telegram Helper . send New Map Location ( share Info , Ġit ) Ċĉĉĉĉĉĉ ĉ } Ċĉĉĉĉĉĉ ĉ app . location Messages . remove Buffered Message ( it ) Ċĉĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉĉ ĉ send Location To Bot ( it , Ġshare Info , ĠSHARE _ TYPE _ MAP ) Ċĉĉĉĉ ĉ } Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉĉ ĉ return Ġapp . location Messages . get Buffered Messages Count For Chat ( chat Id ) Ċĉ ĉ } Ċĉ ĉ return Ġ 0 Ċ ĉ } ĊĊ ĉ fun Ġstart Sharing Location () Ġ{ Ċĉ ĉ if Ġ(! sharing Location ) Ġ{ Ċĉĉ ĉ sharing Location Ġ= Ġtrue ĊĊĉĉ ĉ app . start My Location Service () ĊĊĉĉ ĉ refresh Notification () ĊĊĉĉ ĉ if Ġ( app . telegram Service Ġ!= Ġnull ) Ġ{ Ċĉĉĉ ĉ check And Send Buffer Messages () Ċĉĉ ĉ } Ċĉ ĉ } Ġelse Ġ{ Ċĉĉ ĉ app . force Update My Location () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġstop Sharing Location () Ġ{ Ċĉ ĉ if Ġ( sharing Location ) Ġ{ Ċĉĉ ĉ sharing Location Ġ= Ġfalse ĊĊĉĉ ĉ app . stop My Location Service () Ċĉĉ ĉ last Location Ġ= Ġnull Ċĉĉ ĉ last TimeIn Millis Ġ= Ġ 0 L Ċĉĉ ĉ distance Ġ= Ġ 0 Ċĉĉ ĉ duration Ġ= Ġ 0 ĊĊĉĉ ĉ refresh Notification () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġpause Sharing Location () Ġ{ Ċĉ ĉ sharing Location Ġ= Ġfalse ĊĊĉ ĉ app . stop My Location Service () Ċĉ ĉ last Location Ġ= Ġnull Ċĉ ĉ last TimeIn Millis Ġ= Ġ 0 L ĊĊĉ ĉ refresh Notification () Ċ ĉ } ĊĊĊ ĉ private Ġfun Ġshare Location Messages ( location : ĠLocation , ĠuserId : ĠInt ) Ġ{ Ċĉ ĉ val Ġch ats Share Info Ġ= Ġapp . settings . get Ch ats Share Info () Ċĉ ĉ val Ġlatitude Ġ= Ġlocation . latitude Ċĉ ĉ val Ġlongitude Ġ= Ġlocation . longitude Ċĉ ĉ val Ġalt itude Ġ= Ġlocation . altitude Ċĉ ĉ val Ġspeed Ġ= Ġlocation . speed . toDouble () Ċĉ ĉ val Ġaccuracy Ġ= Ġlocation . accuracy . toDouble () Ċĉ ĉ val Ġbe aring Ġ= Ġlocation . be aring . toDouble () Ċĉ ĉ val Ġtime Ġ= Ġlocation . time Ċĉ ĉ val Ġis Bot Ġ= Ġapp . settings . current Sharing Mode Ġ!= ĠuserId . toString () Ċĉ ĉ val Ġdevice Name Ġ= Ġif Ġ( is Bot ) Ġapp . settings . current Sharing Mode Ġelse Ġ"" Ċĉ ĉ var Ġpending Messages Limit Reached Ġ= Ġfalse Ċĉ ĉ val ĠcurrentTime Ġ= ĠSystem . currentTimeMillis () ĊĊĉ ĉ app . location Messages . add My Location Message ( location ) ĊĊĉ ĉ if Ġ( currentTime Ġ- Ġlast Share Location Message Time Ġ< ĠMIN _ MESSAGE _ SEND ING _ INTERVAL _ MS ) Ġ{ Ċĉĉ ĉ return Ċĉ ĉ } Ċĉ ĉ last Share Location Message Time Ġ= ĠcurrentTime ĊĊĉ ĉ ch ats Share Info . values . forEach Ġ{ Ġshare Info Ġ-> Ċĉĉ ĉ val Ġhas Buffered Messages Ġ= Ġcheck And Send Buffer Messages To Chat ( share Info . chat Id ) Ġ> Ġ 0 Ċĉĉ ĉ if Ġ( share Info . is Pending Messages Limit Reached ()) Ġ{ Ċĉĉĉ ĉ pending Messages Limit Reached Ġ= Ġtrue Ċĉĉ ĉ } Ċĉĉ ĉ when Ġ( app . settings . share Type Value ) Ġ{ Ċĉĉĉ ĉ SHARE _ TYPE _ MAP Ġ-> Ġ{ Ċĉĉĉĉ ĉ val Ġmessage Ġ= ĠBuffer Message ( share Info . chat Id , Ġlatitude , Ġlongitude , Ġalt itude , Ġspeed , Ġaccuracy , Ġbe aring , Ġtime , ĠLocation Messages . TYPE _ MAP , Ġdevice Name ) Ċĉĉĉĉ ĉ prepare Map Message ( share Info , Ġmessage , Ġis Bot , Ġhas Buffered Messages ) Ċĉĉĉ ĉ } Ċĉĉĉ ĉ SHARE _ TYPE _ TEXT Ġ-> Ġ{ Ċĉĉĉĉ ĉ val Ġmessage Ġ= ĠBuffer Message ( share Info . chat Id , Ġlatitude , Ġlongitude , Ġalt itude , Ġspeed , Ġaccuracy , Ġbe aring , Ġtime , ĠLocation Messages . TYPE _ TEXT , Ġdevice Name ) Ċĉĉĉĉ ĉ prepare Text Message ( share Info , Ġmessage , Ġis Bot , Ġhas Buffered Messages ) Ċĉĉĉ ĉ } Ċĉĉĉ ĉ SHARE _ TYPE _ MAP _ AND _ TEXT Ġ-> Ġ{ Ċĉĉĉĉ ĉ val Ġmessage Map Ġ= ĠBuffer Message ( share Info . chat Id , Ġlatitude , Ġlongitude , Ġalt itude , Ġspeed , Ġaccuracy , Ġbe aring , Ġtime , ĠLocation Messages . TYPE _ MAP , Ġdevice Name ) Ċĉĉĉĉ ĉ val Ġmessage Text Ġ= ĠBuffer Message ( share Info . chat Id , Ġlatitude , Ġlongitude , Ġalt itude , Ġspeed , Ġaccuracy , Ġbe aring , Ġtime , ĠLocation Messages . TYPE _ TEXT , Ġdevice Name ) Ċĉĉĉĉ ĉ prepare Map Message ( share Info , Ġmessage Map , Ġis Bot , Ġhas Buffered Messages ) Ċĉĉĉĉ ĉ prepare Text Message ( share Info , Ġmessage Text , Ġis Bot , Ġhas Buffered Messages ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ċĉ ĉ if Ġ( pending Messages Limit Reached ) Ġ{ Ċĉĉ ĉ update Network Type () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġprepare Text Message ( share Info : ĠTelegram Settings . Share Chat Info , Ġmessage : ĠBuffer Message , Ġis Bot : ĠBoolean , Ġhas Buffered Messages : ĠBoolean ) Ġ{ Ċĉ ĉ log . debug (" prepare Text Message Ġ$ message ") Ċĉ ĉ if Ġ( share Info . current Text MessageId Ġ== Ġ- 1 L ) Ġ{ Ċĉĉ ĉ if Ġ( share Info . pending Text Message ) Ġ{ Ċĉĉĉ ĉ app . location Messages . add Buffered Message ( message ) Ċĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉ ĉ if Ġ( is Bot ) Ġ{ Ċĉĉĉĉ ĉ send Location To Bot ( message , Ġshare Info , ĠSHARE _ TYPE _ TEXT ) Ċĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉ ĉ val Ġcontent Ġ= ĠOs mand Location Utils . getText Message Content ( share Info . update Text MessageId , Ġmessage , Ġapp ) Ċĉĉĉĉ ĉ app . telegram Helper . send New Text Location ( share Info , Ġcontent ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ġelse Ġ{ Ċĉĉ ĉ if Ġ( is Bot ) Ġ{ Ċĉĉĉ ĉ if Ġ( app . is Internet Connection Available ) Ġ{ Ċĉĉĉĉ ĉ send Location To Bot ( message , Ġshare Info , ĠSHARE _ TYPE _ TEXT ) Ċĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉ ĉ app . location Messages . add Buffered Message ( message ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉ ĉ if Ġ(! share Info . is Pending Text Messages Limit Reached () Ġ&& Ġ! has Buffered Messages ) Ġ{ Ċĉĉĉĉ ĉ val Ġcontent Ġ= ĠOs mand Location Utils . getText Message Content ( share Info . update Text MessageId , Ġmessage , Ġapp ) Ċĉĉĉĉ ĉ app . telegram Helper . edit Text Location ( share Info , Ġcontent ) Ċĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉ ĉ app . location Messages . add Buffered Message ( message ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġprepare Map Message ( share Info : ĠTelegram Settings . Share Chat Info , Ġmessage : ĠBuffer Message , Ġis Bot : ĠBoolean , Ġhas Buffered Messages : ĠBoolean ) Ġ{ Ċĉ ĉ log . debug (" prepare Map Message Ġ$ message ") Ċĉ ĉ if Ġ( share Info . current Map MessageId Ġ== Ġ- 1 L ) Ġ{ Ċĉĉ ĉ if Ġ( share Info . pending Map Message ) Ġ{ Ċĉĉĉ ĉ app . location Messages . add Buffered Message ( message ) Ċĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉ ĉ if Ġ( is Bot ) Ġ{ Ċĉĉĉĉ ĉ if Ġ( app . is Internet Connection Available ) Ġ{ Ċĉĉĉĉĉ ĉ send Location To Bot ( message , Ġshare Info , ĠSHARE _ TYPE _ MAP ) Ċĉĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉĉ ĉ app . location Messages . add Buffered Message ( message ) Ċĉĉĉĉ ĉ } Ċĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉ ĉ app . telegram Helper . send New Map Location ( share Info , Ġmessage ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ġelse Ġ{ Ċĉĉ ĉ if Ġ( is Bot ) Ġ{ Ċĉĉĉ ĉ if Ġ( app . is Internet Connection Available ) Ġ{ Ċĉĉĉĉ ĉ send Location To Bot ( message , Ġshare Info , ĠSHARE _ TYPE _ MAP ) Ċĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉ ĉ app . location Messages . add Buffered Message ( message ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉ ĉ if Ġ(! share Info . is Pending Map Messages Limit Reached () Ġ&& Ġ! has Buffered Messages ) Ġ{ Ċĉĉĉĉ ĉ app . telegram Helper . edit Map Location ( share Info , Ġmessage ) Ċĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉ ĉ app . location Messages . add Buffered Message ( message ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġupdate Network Type (){ Ċĉ ĉ if Ġ( app . is Internet Connection Available ) Ġ{ Ċĉĉ ĉ val Ġnetwork Type Ġ= Ġwhen Ġ{ Ċĉĉĉ ĉ app . is Wifi Connected Ġ-> ĠT d Api . Network Type WiFi () Ċĉĉĉ ĉ app . is Mobile Connected Ġ-> ĠT d Api . Network Type Mobile () Ċĉĉĉ ĉ else Ġ-> ĠT d Api . Network Type Other () Ċĉĉ ĉ } Ċĉĉ ĉ app . telegram Helper . network Change ( network Type ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġsend Location To Bot ( location Message : ĠBuffer Message , Ġshare Info : ĠTelegram Settings . Share Chat Info , Ġshare Type : ĠString ) Ġ{ Ċĉ ĉ if Ġ( app . is Internet Connection Available ) Ġ{ Ċĉĉ ĉ log . debug (" send Location To Bot Ġ${ location Message . device Name }") Ċĉĉ ĉ val Ġurl Ġ= Ġget Device Sharing Url ( location Message , Ġlocation Message . device Name ) Ċĉĉ ĉ if Ġ( share Type Ġ== ĠSHARE _ TYPE _ TEXT ) Ġ{ Ċĉĉĉ ĉ share Info . last Send Text Message Time Ġ= Ġ( System . currentTimeMillis () Ġ/ Ġ 1 0 0 0 ). toInt () Ċĉĉ ĉ } Ġelse Ġif Ġ( share Type Ġ== ĠSHARE _ TYPE _ MAP ) Ġ{ Ċĉĉĉ ĉ share Info . last Send Map Message Time Ġ= Ġ( System . currentTimeMillis () Ġ/ Ġ 1 0 0 0 ). toInt () Ċĉĉ ĉ } Ċĉĉ ĉ Android Network Utils . send Request Async ( app , Ġurl , Ġnull , Ġ" Send ĠLocation ", Ġfalse , Ġfalse , Ċĉĉĉ ĉ object Ġ: ĠAndroid Network Utils . On Request Result Listener Ġ{ Ċĉĉĉĉ ĉ override Ġfun Ġon Result ( result : ĠString ?) Ġ{ Ċĉĉĉĉĉ ĉ val Ġsuccess Ġ= Ġcheck Result ( result ) Ċĉĉĉĉĉ ĉ val Ġos mand Bot Id Ġ= Ġapp . telegram Helper . get Os mand Bot ()?. id Ġ?: Ġ- 1 Ċĉĉĉĉĉ ĉ val Ġdevice Ġ= Ġapp . settings . getCurrent Sharing Device () Ċĉĉĉĉĉ ĉ if Ġ( success ) Ġ{ Ċĉĉĉĉĉĉ ĉ share Info . sent Messages ++ Ċĉĉĉĉĉĉ ĉ if Ġ( share Type Ġ== ĠSHARE _ TYPE _ TEXT ) Ġ{ Ċĉĉĉĉĉĉĉ ĉ share Info . last Text Successful Send Time Ġ= ĠSystem . currentTimeMillis () Ġ/ Ġ 1 0 0 0 Ċĉĉĉĉĉĉ ĉ } Ġelse Ġif Ġ( share Type Ġ== ĠSHARE _ TYPE _ MAP ) Ġ{ Ċĉĉĉĉĉĉĉ ĉ share Info . last Map Successful Send Time Ġ= ĠSystem . currentTimeMillis () Ġ/ Ġ 1 0 0 0 Ċĉĉĉĉĉĉ ĉ } Ċĉĉĉĉĉĉ ĉ app . location Messages . remove Buffered Message ( location Message ) Ċĉĉĉĉĉĉ ĉ if Ġ(( share Info . should Send Via Bot Text Message Ġ|| Ġshare Info . should Send Via Bot Map Message ) Ġ&& Ġos mand Bot Id Ġ!= Ġ- 1 Ġ&& Ġdevice Ġ!= Ġnull ) Ġ{ Ċĉĉĉĉĉĉĉ ĉ app . telegram Helper . send Via Bot Location Message ( os mand Bot Id , Ġshare Info , ĠT d Api . Location ( location Message . lat , Ġlocation Message . lon , Ġlocation Message . hd op ), Ġdevice , Ġshare Type ) Ċĉĉĉĉĉĉĉ ĉ share Info . should Send Via Bot Text Message Ġ= Ġfalse Ċĉĉĉĉĉĉĉ ĉ share Info . should Send Via Bot Map Message Ġ= Ġfalse Ċĉĉĉĉĉĉ ĉ } Ċĉĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ } Ċĉĉĉ ĉ }) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġget Device Sharing Url ( loc : ĠBuffer Message , Ġsharing Mode : ĠString ): ĠString Ġ{ Ċĉ ĉ val Ġurl Ġ= Ġ"$ BASE _ URL / device /$ sharing Mode / send ? lat =${ loc . lat }& lon =${ loc . lon }" Ċĉ ĉ val Ġbuilder Ġ= ĠStringBuilder ( url ) Ċĉ ĉ if Ġ( loc . be aring Ġ!= Ġ 0 . 0 ) Ġ{ Ċĉĉ ĉ builder . append ("& azi =${ loc . be aring }") Ċĉ ĉ } Ċĉ ĉ if Ġ( loc . speed Ġ!= Ġ 0 . 0 ) Ġ{ Ċĉĉ ĉ builder . append ("& spd =${ loc . speed }") Ċĉ ĉ } Ċĉ ĉ if Ġ( loc . altitude Ġ!= Ġ 0 . 0 ) Ġ{ Ċĉĉ ĉ builder . append ("& alt =${ loc . altitude }") Ċĉ ĉ } Ċĉ ĉ if Ġ( loc . hd op Ġ!= Ġ 0 . 0 ) Ġ{ Ċĉĉ ĉ builder . append ("& hd op =${ loc . hd op }") Ċĉ ĉ } Ċĉ ĉ return Ġbuilder . toString () Ċ ĉ } ĊĊ ĉ private Ġfun Ġcheck Result ( result : ĠString ?): ĠBoolean Ġ{ Ċĉ ĉ if Ġ( result Ġ!= Ġnull ) Ġ{ Ċĉĉ ĉ try Ġ{ Ċĉĉĉ ĉ val Ġjson Result Ġ= ĠJSONObject ( result ) Ċĉĉĉ ĉ val Ġstatus Ġ= Ġjson Result . getString (" status ") Ċĉĉĉ ĉ return Ġstatus Ġ== Ġ" OK " Ċĉĉ ĉ } Ġcatch Ġ( e : ĠJSON Exception ) Ġ{ Ċĉĉ ĉ } Ċĉ ĉ } Ċĉ ĉ return Ġfalse Ċ ĉ } ĊĊ ĉ private Ġfun Ġrefresh Notification () Ġ{ Ċĉ ĉ app . run In UIT hread Ġ{ Ċĉĉ ĉ app . notification Helper . refresh Notification ( Notification Type . LOCATION ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ comp anion Ġobject Ġ{ ĊĊĉ ĉ const Ġval ĠMAX _ MESSAGES _ IN _ TD LIB _ PER _ CHAT Ġ= Ġ 5 ĊĊĉ ĉ // Ġmin Ġand Ġmax Ġvalues Ġfor Ġthe ĠUI Ċĉ ĉ const Ġval ĠMIN _ LOCATION _ MESSAGE _ LI VE _ PERIOD _ SEC Ġ= ĠTelegram Helper . MIN _ LOCATION _ MESSAGE _ LI VE _ PERIOD _ SEC Ġ- Ġ 1 Ċĉ ĉ const Ġval ĠMAX _ LOCATION _ MESSAGE _ LI VE _ PERIOD _ SEC Ġ= ĠTelegram Helper . MAX _ LOCATION _ MESSAGE _ LI VE _ PERIOD _ SEC Ġ+ Ġ 1 Ċ ĉ } Ċ }