Created
September 26, 2017 15:13
-
-
Save anonymous/8fac7650b34aa19229d5f6b91d2454d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mbc.analytics.sdk.services; | |
import android.app.AppOpsManager; | |
import android.app.job.JobParameters; | |
import android.app.job.JobService; | |
import android.app.usage.UsageEvents; | |
import android.app.usage.UsageStatsManager; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.net.ConnectivityManager; | |
import android.os.Build; | |
import android.support.annotation.RequiresApi; | |
import org.json.JSONArray; | |
import mbc.analytics.sdk.agent.MbcAnalytics; | |
import mbc.analytics.sdk.constants.Constants; | |
import mbc.analytics.sdk.logs.Logger; | |
import mbc.analytics.sdk.network.NetworkDataManager; | |
import mbc.analytics.sdk.room.database.DatabaseRepository; | |
import mbc.analytics.sdk.room.entity.AppRelation; | |
import mbc.analytics.sdk.sharepref.SessionManager; | |
import mbc.analytics.sdk.task.ConnectionTask; | |
import mbc.analytics.sdk.utils.Utils; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.List; | |
import java.util.Locale; | |
import java.util.TimeZone; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1) | |
public class LollipopService extends JobService implements NetworkDataManager.NetworkDataManagerCallBack { | |
private DatabaseRepository databaseRepository; | |
private String TempPackage; | |
private int TempPackageHashCode; | |
private NetworkDataManager networkDataManager; | |
private ExecutorService mExecutorService; | |
private String uuid; | |
private boolean killSwitch; | |
private boolean pauseSwitch; | |
private SessionManager mSessionManager; | |
private static final Object LOCK = new Object(); | |
/** | |
* BroadCastReceiver that check the connection on Device. | |
* Saves the Results in DB (Boolean). | |
*/ | |
private BroadcastReceiver ConnectionInfoReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent connectionIntent) { | |
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
if (cm.getActiveNetworkInfo() != null) { | |
Logger.info("NetWorkBroadcast is Connected : " + Boolean.TRUE); | |
mExecutorService.execute(new ConnectionTask(databaseRepository,true)); | |
} else { | |
Logger.info("NetWorkBroadcast is Connected : " + Boolean.FALSE); | |
mExecutorService.execute(new ConnectionTask(databaseRepository,false)); | |
} | |
} | |
}; | |
/** | |
* Default Constructor. Initialize DB and saves data to SharePref. | |
* UsageStatRepository (Application's stats) | |
* NetworkDataManager ( Network Manager) | |
*/ | |
public LollipopService() { | |
super(); | |
************ | |
mExecutorService = Executors.newSingleThreadExecutor(); | |
} | |
/** | |
* Register the ConnectionReceiver. | |
*/ | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
Logger.info("Service onCreate"); | |
/* if App has Usage Permission enable *//* | |
if (!hasPermission(getApplicationContext())) { | |
stopService(); | |
}*/ | |
} | |
/** | |
* This Method will call on Job finish and unregister all listeners. | |
*/ | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
Logger.info("Service destroyed"); | |
try { | |
unregisterReceiver(ConnectionInfoReceiver); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} | |
if(mExecutorService!=null) | |
mExecutorService.shutdown(); | |
/*public void databaseClose() { | |
if (db.isOpen()) { | |
db.close(); | |
} | |
}*/ | |
if(databaseRepository!=null) | |
databaseRepository.databaseClose(); | |
if(networkDataManager!=null) | |
networkDataManager.onDestroy(); | |
} | |
/** | |
* When job start | |
* gets the latest SuccessSendDay, to init the Range of Stats (SuccessSendDay , NOW) | |
* On First Run SuccessSendDay is equals with NOW | |
*/ | |
@Override | |
public boolean onStartJob(JobParameters params) { | |
mExecutorService.execute(new Runnable() { | |
@Override | |
public void run() { | |
synchronized (LOCK) { | |
/* if SuccessSendDay is equals with 0 (Default) */ | |
******** | |
long startingTime = mSessionManager.getLongPreference("startRange"); | |
if (startingTime != 0) { | |
**** | |
databaseRepository = new DatabaseRepository(getApplicationContext()); | |
networkDataManager = new NetworkDataManager(Constants.KEY, databaseRepository, mSessionManager, LollipopService.this); | |
if (!databaseRepository.dataKeyExists(Constants.KEY)) { | |
*************** | |
********* | |
databaseRepository.createDataEntity(Constants.KEY, uuid, System.currentTimeMillis(), secToTrack); | |
databaseRepository.setSendDataInterval(Constants.KEY, TimeUnit.SECONDS.toMillis(sendDataTimeInterval)); | |
databaseRepository.setStatusOfService(Constants.KEY, killSwitch, pauseSwitch); | |
} | |
registerReceiver(ConnectionInfoReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); | |
getStats(startingTime); | |
} else { | |
stopService(); | |
} | |
} | |
} | |
}); | |
return true; | |
} | |
/** | |
* When Job Stops | |
*/ | |
@Override | |
public boolean onStopJob(JobParameters params) { | |
// Note: return true to reschedule this job. | |
Logger.info("onStopJob"); | |
return false; | |
} | |
/** | |
* Main job process | |
*/ | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1) | |
private void getStats(long startTime) { | |
/* if Kill switch is true then kill the service */ | |
if (!databaseRepository.getKillSwitch(Constants.KEY)) { | |
/* if Pause switch is true stop collect data */ | |
if (!databaseRepository.getPauseSwitch(Constants.KEY)) { | |
************** | |
/* for every event */ | |
while (usageEvents.hasNextEvent()) { | |
********* | |
if (event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) { | |
Logger.info("Package : " + event.getPackageName() + " time " + Utils.getDate(event.getTimeStamp())); | |
if (mSessionManager.getStringPreference(Constants.TEMP_PACKAGE).equals("") || mSessionManager.getStringPreference(Constants.TEMP_PACKAGE).equals("n/a")) { | |
*********** | |
********** | |
databaseRepository.createAppEntity(TempPackage, 0, Utils.getDate(event.getTimeStamp())); | |
} else { | |
if (!event.getPackageName().equals(mSessionManager.getStringPreference(Constants.TEMP_PACKAGE))) { | |
********* | |
databaseRepository.createAppEntity(TempPackage, 0, Utils.getDate(event.getTimeStamp())); | |
********** | |
******** | |
databaseRepository.createAppEntity(TempPackage, 0, Utils.getDate(event.getTimeStamp())); | |
} | |
} | |
} | |
} | |
if (networkDataManager != null) | |
networkDataManager.sendData(System.currentTimeMillis(), getApplicationContext()); | |
} | |
} else { | |
stopService(); | |
} | |
} | |
@Override | |
public void stopService() { | |
} | |
@Override | |
public void updateService() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment