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
public class MainViewModel extends AndroidViewModel { | |
private LiveData<List<StarWarsMovie>> moviesLiveData; | |
@Inject AppDatabase mDb; | |
public MainViewModel(Application application) { | |
super(application); | |
((StarWarsApplication) getApplication()).getAppComponent().inject(this); | |
requestDataUpdates(); |
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
public class StarWarsService extends IntentService { | |
@Inject AppDatabase db; | |
public StarWarsService() { | |
super("StarWarsService"); | |
} | |
@Override public void onCreate() { | |
super.onCreate(); |
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
public abstract class BasePresenter<T extends MvpView> { | |
HandlerThread thread; | |
Looper looper; | |
Handler handler; | |
private T mView; | |
public BasePresenter() { | |
} |
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
Job myJob = firebaseJobDispatcher.newJobBuilder() | |
.setService(SmartService.class) | |
.setTag(SmartService.LOCATION_SMART_JOB) | |
.setReplaceCurrent(false) | |
.setConstraints(ON_ANY_NETWORK) | |
.build(); | |
firebaseJobDispatcher.mustSchedule(myJob); |
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
@Override | |
public WorkerResult doWork() { | |
... | |
LocalBroadcastManager.getInstance(getAppContext()).registerReceiver(receiver, new IntentFilter(LocationBaseBroadcast.ACTION_NEW_LOCATION_ARRIVED)); | |
locationTracker.start(); | |
try { | |
locationWait = new CountDownLatch(1); | |
locationWait.await(); | |
} catch (InterruptedException e) { |
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
public class LocationWork extends Worker { | |
... | |
static void reportFinished() { | |
if (locationWait != null) { | |
Log.d(TAG, "doWork: locationWait down by one"); | |
locationWait.countDown(); | |
} | |
} |
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
//location obtained, need to send it to server | |
private void broadcastLocation(Location location) { | |
//release latch | |
reportFinished(); | |
//We need to sure that device have internet live | |
Constraints constraints = new Constraints.Builder().setRequiredNetworkType | |
(NetworkType.CONNECTED).build(); | |
//Parse our location to Data to use it as input for our worker | |
Data inputData = new Data.Builder() |
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
public class LocationUploadWorker extends Worker { | |
//... | |
public WorkerResult doWork() { | |
//get Data out from input | |
double longitude = getInputData().getDouble(LOCATION_LONG, 0); | |
double latitude = getInputData().getDouble(LOCATION_LAT, 0); | |
long time = getInputData().getLong(LOCATION_TIME, 0); | |
String osVersion = "WorkerReport v" + Build.VERSION.SDK_INT; | |
//construct our report for server format |
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
int threads = Runtime.getRuntime().availableProcessors(); | |
ExecutorService executor = Executors.newFixedThreadPool(threads); | |
executor.submit(myWork); |