-
Notifications
You must be signed in to change notification settings - Fork 209
Make connection to CodeGeneratorService lazy. #509
Changes from 2 commits
8d9f6a0
416f552
c1e534d
296e5c8
c64980b
c72b767
5a895d9
63467a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,92 +5,87 @@ | |
import android.content.Intent; | ||
import android.content.ServiceConnection; | ||
import android.os.IBinder; | ||
import android.util.Log; | ||
import android.widget.Toast; | ||
|
||
import com.google.blockly.android.codegen.CodeGenerationRequest.CodeGeneratorCallback; | ||
import com.google.blockly.model.BlocklySerializerException; | ||
import com.google.blockly.model.Workspace; | ||
import com.google.blockly.utils.StringOutputStream; | ||
|
||
import java.util.List; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
/** | ||
* Client Side class responsible for connecting to the {@link CodeGeneratorService}. | ||
* | ||
* A connection to the service is only made the first time code generation is requested. | ||
*/ | ||
public class CodeGeneratorManager { | ||
private static final String TAG = "CodeGeneratorManager"; | ||
|
||
private final Context mContext; | ||
private final ServiceConnection mCodeGenerationConnection; | ||
private final Queue<CodeGenerationRequest> mStoredRequests; | ||
|
||
private ServiceConnection mCodeGenerationConnection; | ||
private CodeGeneratorService mGeneratorService; | ||
|
||
public CodeGeneratorManager(Context context) { | ||
this.mContext = context; | ||
|
||
this.mCodeGenerationConnection = new ServiceConnection() { | ||
@Override | ||
public void onServiceConnected(ComponentName className, IBinder binder) { | ||
mGeneratorService = | ||
((CodeGeneratorService.CodeGeneratorBinder) binder).getService(); | ||
} | ||
|
||
@Override | ||
public void onServiceDisconnected(ComponentName arg0) { | ||
mGeneratorService = null; | ||
} | ||
}; | ||
this.mStoredRequests = new LinkedList<>(); | ||
} | ||
|
||
/** | ||
* Resumes the underlying code generation connection. | ||
*/ | ||
public void onResume() { | ||
Intent intent = new Intent(mContext, CodeGeneratorService.class); | ||
mContext.bindService(intent, mCodeGenerationConnection, Context.BIND_AUTO_CREATE); | ||
if (mCodeGenerationConnection != null) { | ||
mContext.bindService(intent, mCodeGenerationConnection, Context.BIND_AUTO_CREATE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear, it is this bind that could be lazy, and is the point of issue #37. We should wait until the first There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. I've made an update so that the bind is lazy (rather than creation of the connection). |
||
} | ||
} | ||
|
||
/** | ||
* Pauses the underlying code generation connection. | ||
*/ | ||
public void onPause() { | ||
mContext.unbindService(mCodeGenerationConnection); | ||
if (mCodeGenerationConnection != null) { | ||
mContext.unbindService(mCodeGenerationConnection); | ||
} | ||
} | ||
|
||
/** | ||
* Calls the Service to request code generation for the workspace passed in. | ||
* | ||
* @param workspace the workspace to request code generation for. | ||
* @param codeGenerationCallback the callback to call with the generated code. | ||
* @param codeGenerationRequest the request to generate code. | ||
*/ | ||
public void requestCodeGeneration(List<String> blockDefinitionsJsonPaths, | ||
List<String> generatorsJsPaths, | ||
Workspace workspace, | ||
CodeGeneratorCallback codeGenerationCallback) { | ||
|
||
if (workspace.hasBlocks()) { | ||
if (isBound()) { | ||
try { | ||
final StringOutputStream serialized = new StringOutputStream(); | ||
workspace.serializeToXml(serialized); | ||
|
||
mGeneratorService.requestCodeGeneration( | ||
new CodeGenerationRequest( | ||
serialized.toString(), | ||
codeGenerationCallback, | ||
blockDefinitionsJsonPaths, | ||
generatorsJsPaths) | ||
); | ||
} catch (BlocklySerializerException e) { | ||
Log.wtf(TAG, e); | ||
Toast.makeText(mContext, "Code generation failed.", | ||
Toast.LENGTH_LONG).show(); | ||
|
||
} | ||
} else { | ||
Log.i(TAG, "Generator not bound to service. Skipping run request."); | ||
} | ||
public void requestCodeGeneration(CodeGenerationRequest codeGenerationRequest) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nulls are already handled in the code, but I'll add a null check here so that that's clearer. |
||
if (isBound()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add if(!mResumed) { Log.w(TAG, "Code generation called while paused. Request ignored."); return;} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check resumed state first. |
||
executeCodeGenerationRequest(codeGenerationRequest); | ||
} else { | ||
Log.i(TAG, "No blocks in workspace. Skipping run request."); | ||
mStoredRequests.add(codeGenerationRequest); | ||
connectToService(); | ||
} | ||
} | ||
|
||
private boolean isBound() { | ||
return mGeneratorService != null; | ||
} | ||
|
||
private void connectToService() { | ||
this.mCodeGenerationConnection = new ServiceConnection() { | ||
@Override | ||
public void onServiceConnected(ComponentName className, IBinder binder) { | ||
mGeneratorService = ((CodeGeneratorService.CodeGeneratorBinder) binder).getService(); | ||
while (!mStoredRequests.isEmpty()) { | ||
executeCodeGenerationRequest(mStoredRequests.poll()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've used |
||
} | ||
} | ||
|
||
@Override | ||
public void onServiceDisconnected(ComponentName arg0) { | ||
mGeneratorService = null; | ||
} | ||
}; | ||
|
||
} | ||
|
||
private void executeCodeGenerationRequest(CodeGenerationRequest request) { | ||
if (request != null) { | ||
mGeneratorService.requestCodeGeneration(request); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this is going to have merge conflicts with some upcoming changes, I've updated the PR to target the
toolbox-refactor
branch. I'd appreciate if I can have write access to this branch to fix the upcoming conflict.Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I've done this, but let me know if there are still issues (the merge conflicts were all imports, so trivial to resolve).