-
Notifications
You must be signed in to change notification settings - Fork 258
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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"; | ||
|
@@ -95,6 +99,7 @@ protected void onCreate(Bundle savedInstanceState) { | |
|
||
url = connectionData.url; | ||
username = connectionData.username; | ||
password = connectionData.password; | ||
|
||
fillOutData = true; | ||
} catch(IllegalArgumentException e) { | ||
|
@@ -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://"; | ||
|
@@ -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"); | ||
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) { | ||
|
@@ -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(); | ||
|
@@ -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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Does that enforce a specific QR scanner client ?
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.
Yup.It seems that it might not, but I didn't test it.