I am trying to build an app that will rely on a Webservice to work. To make it, I decided to follow the Model-View-ViewModel architecture together with the Repository pattern. I try to make this architecture inspired by the guidelines shown in Guide to App architecture, from Android Developer's Official Site.
I use OkHttp library to consume the WebService, and Room for the phone's database.
I made some testing to see if the app obtained succesfully the Data through the Webservice, from inside the Main Activity, and it worked; the App received successfully the Data.
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
connected = false;
Log.i("MainActivity", "MyWebService DISconnected successfully.");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myweb_service = ((MyWebService.LocalBinder)service).getService();
connected = true;
Log.i("MainActivity", "MyWebService connected successfully.");
}
};
void doBindMyWebService() {
if (bindService(new Intent(this, MyWebService.class),
connection, Context.BIND_AUTO_CREATE))
{
mShouldUnbind = true;
}
else {
Log.e("MainActivity", "ERROR --> Service instance doesn't exist, or this Client doesn't have permission to access.");
}
}
void doUnbindMyWebService() {
if (mShouldUnbind) {
unbindService(connection);
}
}
doBindMyWebService();
/* I am not trying to extract this method at all; I just included it for
* the sake of completeness
*/
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindMyWebService();
}
Now, in order to stick to the chosen architecture, I am trying to move the code that creates the ServiceConnection in the Main Activity, outside the Activity, in order to decouple it from View logic. To do so, I tried wrapping the code that makes the ServiceConnection and binds it into a new class for handling Services. While extracting the code and tried to engage it with the Repository class, the next question came out: is it neccesary binding the Service in order to make it work, and retrieve data from it, or instead, I can do so without binding it at all? (For the record: this is not the question itself; I'm just pointing out what I was thinking while making the code)
Please notice that I am aware that I could just pass some context (the MainActivity's context or the Application context, for example) to the classes' constructors and store it in some property, but this is indeed what I am trying to avoid doing in order to prevent memory leaks. Long story short:
- The above code works within MainActivity
- I am trying to activate Services outside the Activity class, so I made a dedicated class to handle Services within it. I called this class "RemoteDataSource"
- I am trying to make the Repository class to retrieve the data from the Service, once the Service has completed the HTTP call and received the response.
- I want MyWebService to keep running, even if there isn't any Activity instanced.
- Trying to bind the Service to some context under these conditions seems pointless, because it might lead to memory leaks.
- It seems that calling onStartCommand isn't enough to make it run the Service.
Here's the code of MyWebService:
package com.example.myapp;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import com.example.myapp.model.MyDataModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MyWebService extends Service {
private String url= "https://someserver.here/api/data/read.php";
private String response = "";
private JSONArray response_jsonarray;
private boolean running = false;
final static int MESSAGE = 1;
private final IBinder mBinder = new LocalBinder();
public boolean isRunning() {
return this.running;
}
public class LocalBinder extends Binder {
MyWebService getService() {
return MyWebService.this;
}
}
public String getResponse() {
if (!running) return null;
return this.response;
}
public JSONArray getResponseAsJsonArray() {
if (!running) return null;
return this.response_jsonarray;
}
public List<MyDataModel> getResponseAsObject() {
if (!running) return null;
ArrayList<MyDataModel> child_nodes = new ArrayList<MyDataModel>();
for (int i = 0; i < this.response_jsonarray.length(); i++) {
try {
JSONObject parent_obj = this.response_jsonarray.getJSONObject(i);
long parent_id = parent_obj.getLong("id_parent");
short type_id = (short)parent_obj.getInt("type");
short status_id = (short)parent_obj.getInt("status_id");
JSONObject childs_list = parent_obj.getJSONObject("childs");
Iterator<String> iter_childs = childs_list.keys();
while (iter_childs.hasNext()) {
JSONObject child_obj = childs_list.getJSONObject(iter_child.next());
long i_id = child_obj.getLong("id");
String i_name = child_obj.getString("name");
double i_lat = child_obj.getDouble("lat");
double i_long = child_obj.getDouble("long");
short i_order = (short)child_obj.getInt("order");
MyDataModel i_child = new MyDataModel(i_id, i_name, i_lat, i_long, 0, 0, type_id, status_id, 0, parent_id, i_order);
child_nodes.add(i_child);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return child_nodes;
}
/*
* SERVICE LIFECYCLE
*/
@Override
public void onCreate() {
Log.i("MyWebService", "OnCreate TRIGGERED");
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("MyWebService", "ERROR --> " + e.getMessage());
e.printStackTrace();
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("MyWebService", "GOOD -> Response received! :-)");
final String myResponse = response.body().string();
response = myResponse;
try {
response_jsonarray = new JSONArray(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
this.running = true;
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.i("MyWebService", "OnStartCommand TRIGGERED");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("MyWebService", "OnBind TRIGGERED");
return mBinder;
}
}
And here, the code of the class i called "RemoteDataSource":
package com.example.myapp;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
public class RemoteDataSource {
private boolean connected;
private boolean mShouldUnbind;
private MyWebService mWebService;
private final int INTENT_WEBSERVICE = 1;
public boolean isConnected() {
return this.connected;
}
public boolean isBound() {
return this.mShouldUnbind;
}
public ServiceConnection getConnection() {
return this.connection;
}
private ServiceConnection connection;
public MyWebService getMyWebService() {
return this.mWebService;
}
public RemoteDataSource() {
this.connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
connected = false;
Log.i("RemoteDataSource", "MyWebService DISconnected successfully");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mWebService = ((MyWebService.LocalBinder) service).getService();
connected = true;
Log.i("RemoteDataSource", "MyWebService connected successfully");
}
};
}
public boolean isRunning() {
if (this.mWebService == null) return false;
return this.mWebService.isRunning();
}
public void prepareWebService() {
//TODO -> Consider this empty method as a symbol of what I am trying to achieve
}
public void startMyWebService() {
this.mWebService.onStartCommand(new Intent(Intent.ACTION_SYNC), 0, INTENT_WEBSERVICE);
}
public void stopMyWebService() {
this.mWebService.stopSelf();
}
public void doBindMyWebService (Context context) {
if (context.bindService(new Intent(context, MyWebService.class),
this.connection, Context.BIND_AUTO_CREATE)) {
this.mShouldUnbind = true;
} else {
Log.e("RemoteDataSource", "ERROR --> Service instance doesn't exist, or this Client doesn't have permission to access.");
}
}
public void doUnbindMyWebService (Context context) {
if (this.mShouldUnbind) {
context.unbindService(connection);
}
}
}
And here is some piece of my Repository class' constructor:
public Repository(Application application) {
AppDatabase db = AppDatabase.getDatabase(application);
mParentDao = db.parentDao();
mMyDataModelDao = db.myDataModelDao();
mRemoteDataSource = new RemoteDataSource();
/* XXX It was a bad idea, it gets stuck in an infinite loop; I just had to try anyway
* The point is, if I don't wait until MyWebService has made it's work,
* the app crashes with NullPointerException (or it seems so, at least)
*/
while (!mRemoteDataSource.isRunning()) { continue; }
mRemoteDataSource.startMyWebService();
// [...]
}
My objective at this point is to handle this MyWebService of mine from the RemoteDataSource class, avoiding binding it to a context. The WebService might deserve a dedicated Thread, but I am still a noob with threads; so i'd rather keep it simple until I see it working; later I will worry about improving performance.
In any case, I think I am too confused about how should I start the Service (please notice that I am not too familiar with Services and the way to handle them). Is the way I tried to handle the service wrong? If it is, what am I overlooking? What should I have in mind in order to de-couple MyWebService from the View logic?