Skip to content

Commit

Permalink
Make the FormFillingActivity launchable from a link of the form "odkc…
Browse files Browse the repository at this point in the history
…ollect://form/<form-id>", allowing an entity to be preselected with a query parameter.
  • Loading branch information
zestyping committed Mar 17, 2024
1 parent e49eae1 commit 75eb6c5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
10 changes: 9 additions & 1 deletion collect_app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,15 @@ the specific language governing permissions and limitations under the License.
<activity
android:name=".activities.FormFillingActivity"
android:theme="@style/Theme.Collect.FormEntry"
android:windowSoftInputMode="adjustResize" />
android:windowSoftInputMode="adjustResize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="odkcollect" android:host="form" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
<activity
android:name="org.odk.collect.draw.DrawActivity"
android:screenOrientation="landscape" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.data.SelectOneData;
import org.javarosa.core.model.data.helper.Selection;
import org.javarosa.core.model.instance.InstanceInitializationFactory;
import org.javarosa.core.model.instance.TreeElement;
import org.javarosa.core.model.instance.TreeReference;
Expand All @@ -42,6 +44,7 @@
import org.odk.collect.android.dynamicpreload.ExternalAnswerResolver;
import org.odk.collect.android.dynamicpreload.ExternalDataManager;
import org.odk.collect.android.dynamicpreload.ExternalDataUseCases;
import org.odk.collect.android.exception.JavaRosaException;
import org.odk.collect.android.external.FormsContract;
import org.odk.collect.android.external.InstancesContract;
import org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter;
Expand Down Expand Up @@ -178,6 +181,15 @@ protected FECWrapper doInBackground(Void... ignored) {
* explicitly saved instance is edited via edit-saved-form.
*/
instancePath = loadSavePoint();
} else if (uri.getScheme().equals("odkcollect") && uri.getHost().equals("form")) {
// Launch a form from a browsable web link in the format: odkcollect://form/<form_id>
String formId = uri.getPathSegments().get(0);
List<Form> forms = new FormsRepositoryProvider(Collect.getInstance()).get().getAllByFormId(formId);
if (forms.size() == 0) {
Timber.e(new Error("Form not found for URL: " + uri));
return null;
}
form = forms.get(0);
}

if (form.getFormFilePath() == null) {
Expand Down Expand Up @@ -270,10 +282,37 @@ protected FECWrapper doInBackground(Void... ignored) {
fc.setIndexWaitingForData(idx);
}
}

preselectEntity(fc, uri);

data = new FECWrapper(fc, usedSavepoint);
return data;
}

private void preselectEntity(FormController fc, Uri uri) {
FormIndex saved = fc.getFormIndex();
try {
for (int event = fc.jumpToIndex(FormIndex.createBeginningOfFormIndex());
event != FormEntryController.EVENT_END_OF_FORM;
event = fc.stepToNextEvent(false)) {
FormIndex index = fc.getFormIndex();
TreeReference ref = fc.getFormDef().getChildInstanceRef(index);
if (ref != null) {
String value = uri.getQueryParameter(ref.getNameLast());
if (value != null) {
try {
fc.answerQuestion(index, new SelectOneData(new Selection(value)));
} catch (JavaRosaException e) {
Timber.w("Could not preselect answer " + value + " for question " + ref);
}
}
}
}
} finally {
fc.jumpToIndex(saved);
}
}

private static void unzipMediaFiles(File formMediaDir) {
File[] zipFiles = formMediaDir.listFiles(new FileFilter() {
@Override
Expand Down

0 comments on commit 75eb6c5

Please sign in to comment.