Skip to content

Commit

Permalink
Add QR-code scanner invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
di72nn committed Jan 11, 2017
1 parent b17c44b commit a4088f8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
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 android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
Expand Down Expand Up @@ -34,6 +36,8 @@ public class ConnectionWizardActivity extends AppCompatActivity {

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 @@ -118,6 +122,32 @@ 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, "Unrecognized URI scheme", Toast.LENGTH_SHORT).show();
return;
}

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

private ConnectionData parseLoginData(String connectionUri) {
// wallabag://[email protected]
String prefix = "wallabag://";
Expand All @@ -139,6 +169,25 @@ private ConnectionData parseLoginData(String connectionUri) {
return new ConnectionData(values[0], values[1]);
}

public void scanQrCode() {
try {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
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, "Please install QR-code scanner", Toast.LENGTH_LONG).show();

Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
startActivity(marketIntent);
} catch(Exception e) {
Log.e(TAG, "scanQrCode() exception", e);
}
}

public void prev(WizardPageFragment fragment, Bundle bundle) {
String currentPage = fragment != null ? fragment.getPageName() : PAGE_NONE;

Expand Down Expand Up @@ -312,6 +361,21 @@ 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(new View.OnClickListener() {
@Override
public void onClick(View v) {
activity.scanQrCode();
}
});
}
}

@Override
protected void nextButtonPressed() {
Bundle bundle = getArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
android:textColor="?android:attr/textColorPrimary"
android:text="@string/connectionWizard_providerSelection_titleMessage"/>

<Button
android:id="@+id/scanQrCodeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan QR code" />

<RadioGroup
android:id="@+id/providerRadioGroup"
android:layout_width="wrap_content"
Expand Down

0 comments on commit a4088f8

Please sign in to comment.