Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start QR code scanner from app #369

Merged
merged 1 commit into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package fr.gaulupeau.apps.Poche.ui.preferences;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -36,6 +38,8 @@ public class ConnectionWizardActivity extends BaseActionBarActivity {

private static final String TAG = "ConnectionWizard";

private static final int REQUEST_CODE_QR_CODE = 1;

private static final String DATA_PROVIDER = "provider";
private static final String DATA_URL = "url";
private static final String DATA_USERNAME = "username";
Expand Down Expand Up @@ -95,6 +99,7 @@ protected void onCreate(Bundle savedInstanceState) {

url = connectionData.url;
username = connectionData.username;
password = connectionData.password;

fillOutData = true;
} catch(IllegalArgumentException e) {
Expand Down Expand Up @@ -152,6 +157,34 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == REQUEST_CODE_QR_CODE) {
if(resultCode == RESULT_OK) {
String resultString = data.getStringExtra("SCAN_RESULT");
Log.d(TAG, "onActivityResult() got string: " + resultString);

if(resultString == null) return;

Uri uri = Uri.parse(resultString);
if(!"wallabag".equals(uri.getScheme())) {
Log.i(TAG, "onActivityResult() unrecognized URI scheme: " + uri.getScheme());
Toast.makeText(this, R.string.connectionWizard_misc_incorrectConnectionURI,
Toast.LENGTH_SHORT).show();
return;
}

Intent intent = getIntent();
intent.setData(uri);
intent.removeExtra(EXTRA_FILL_OUT_FROM_SETTINGS);
startActivity(intent);
finish();
}
}
}

private ConnectionData parseLoginData(String connectionUri) {
// wallabag://[email protected]
String prefix = "wallabag://";
Expand All @@ -170,7 +203,35 @@ private ConnectionData parseLoginData(String connectionUri) {
throw new IllegalArgumentException("Illegal number of login URL elements detected: " + values.length);
}

return new ConnectionData(values[0], values[1]);
String username = values[0];
String password = null;
if(username.contains(":")) {
int index = username.indexOf(":");
password = username.substring(index + 1);
username = username.substring(0, index);
}

return new ConnectionData(username, password, values[1]);
}

private void scanQrCode() {
try {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that enforce a specific QR scanner client ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. It seems that it might not, but I didn't test it.

intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

startActivityForResult(intent, REQUEST_CODE_QR_CODE);
} catch(ActivityNotFoundException e) {
Log.i(TAG, "scanQrCode() exception", e);

Toast.makeText(this, R.string.connectionWizard_misc_installQrCodeScanner,
Toast.LENGTH_LONG).show();

Uri marketUri = Uri.parse("market://details?id=de.markusfisch.android.binaryeye");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
startActivity(marketIntent);
} catch(Exception e) {
Log.w(TAG, "scanQrCode() exception", e);
}
}

public void prev(WizardPageFragment fragment, Bundle bundle) {
Expand Down Expand Up @@ -346,6 +407,16 @@ protected int getLayoutResourceID() {
return R.layout.connection_wizard_provider_selection_fragment;
}

@Override
protected void initButtons(View v) {
super.initButtons(v);

Button scanCodeButton = (Button)v.findViewById(R.id.scanQrCodeButton);
if(scanCodeButton != null) {
scanCodeButton.setOnClickListener(v1 -> activity.scanQrCode());
}
}

@Override
protected void nextButtonPressed() {
Bundle bundle = getArguments();
Expand Down Expand Up @@ -628,10 +699,12 @@ protected void saveSettings() {

private class ConnectionData {
String username;
String password;
String url;

ConnectionData(String username, String url) {
ConnectionData(String username, String password, String url) {
this.username = username;
this.password = password;
this.url = url;
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_qrcode_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000"
android:pathData="M3,11H5V13H3V11M11,5H13V9H11V5M9,11H13V15H11V13H9V11M15,11H17V13H19V11H21V13H19V15H21V19H19V21H17V19H13V21H11V17H15V15H17V13H15V11M19,19V15H17V19H19M15,3H21V9H15V3M17,5V7H19V5H17M3,3H9V9H3V3M5,5V7H7V5H5M3,15H9V21H3V15M5,17V19H7V17H5Z" />
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<RadioGroup
android:id="@+id/providerRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:layout_height="wrap_content"
android:layout_marginBottom="20dp">

<RadioButton
android:id="@+id/providerWallabagIt"
Expand Down Expand Up @@ -62,6 +63,15 @@
android:layout_height="wrap_content"
android:text="@string/connectionWizard_providerSelection_provider_general_desc" />
</RadioGroup>

<Button
android:id="@+id/scanQrCodeButton"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_qrcode_24dp"
android:text="@string/connectionWizard_misc_scanQrCode" />

</LinearLayout>
</ScrollView>

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@
<string name="pref_name_misc_wipeDB_confirmTitle">Wipe Database?</string>
<string name="pref_name_misc_wipeDB_confirmMessage">Are you sure you want to wipe the database?</string>

<string name="connectionWizard_misc_scanQrCode">Scan a QR code</string>
<string name="connectionWizard_misc_installQrCodeScanner">Please install a QR-code scanner</string>
<string name="connectionWizard_misc_incorrectConnectionURI">Incorrect connection URI used</string>

<string name="connectionWizard_welcome_titleMessage">Welcome to wallabag.</string>
Expand Down