QR code scanning

This commit is contained in:
str4d
2014-11-25 03:09:05 +00:00
parent 9b145ad0e1
commit 1a8c6bfe28
10 changed files with 100 additions and 56 deletions

View File

@@ -1,5 +1,6 @@
package i2p.bote.android.addressbook;
import i2p.bote.android.Constants;
import i2p.bote.android.InitActivities;
import i2p.bote.android.R;
import i2p.bote.packet.dht.Contact;
@@ -9,6 +10,9 @@ import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class AddressBookActivity extends ActionBarActivity implements
AddressBookFragment.OnContactSelectedListener {
static final int ALTER_CONTACT_LIST = 1;
@@ -39,7 +43,7 @@ public class AddressBookActivity extends ActionBarActivity implements
@Override
public void onContactSelected(Contact contact) {
if (getIntent().getAction() == Intent.ACTION_PICK) {
if (Intent.ACTION_PICK.equals(getIntent().getAction())) {
Intent result = new Intent();
result.putExtra(ViewContactFragment.CONTACT_DESTINATION, contact.getBase64Dest());
setResult(Activity.RESULT_OK, result);
@@ -53,7 +57,16 @@ public class AddressBookActivity extends ActionBarActivity implements
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ALTER_CONTACT_LIST) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {
String content = scanResult.getContents();
if (content != null && content.startsWith(Constants.EMAILDEST_SCHEME)) {
String destination = content.substring(Constants.EMAILDEST_SCHEME.length() + 1);
Intent nci = new Intent(this, EditContactActivity.class);
nci.putExtra(EditContactFragment.NEW_DESTINATION, destination);
startActivityForResult(nci, ALTER_CONTACT_LIST);
}
} else if (requestCode == ALTER_CONTACT_LIST) {
if (resultCode == Activity.RESULT_OK) {
AddressBookFragment f = (AddressBookFragment) getSupportFragmentManager().findFragmentById(R.id.container);
f.updateContactList();

View File

@@ -15,6 +15,8 @@ import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ListView;
import com.google.zxing.integration.android.IntentIntegrator;
import java.util.SortedSet;
import i2p.bote.I2PBote;
@@ -76,6 +78,14 @@ public class AddressBookFragment extends AuthenticatedListFragment implements
}
});
b = (ImageButton) v.findViewById(R.id.action_scan_qr_code);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startScanQrCode();
}
});
return v;
}
@@ -120,6 +130,11 @@ public class AddressBookFragment extends AuthenticatedListFragment implements
getActivity().startActivityForResult(nci, AddressBookActivity.ALTER_CONTACT_LIST);
}
private void startScanQrCode() {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);
}
@Override
public void onListItemClick(ListView parent, View view, int pos, long id) {
mCallback.onContactSelected(mAdapter.getItem(pos));

View File

@@ -33,11 +33,18 @@ public class EditContactActivity extends ActionBarActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
String destination = null;
EditContactFragment f = null;
Bundle args = getIntent().getExtras();
if (args != null)
destination = args.getString(EditContactFragment.CONTACT_DESTINATION);
EditContactFragment f = EditContactFragment.newInstance(destination);
if (args != null) {
String destination = args.getString(EditContactFragment.NEW_DESTINATION);
if (destination != null) {
String name = args.getString(EditContactFragment.NEW_NAME);
f = EditContactFragment.newInstance(name, destination);
} else {
destination = args.getString(EditContactFragment.CONTACT_DESTINATION);
f = EditContactFragment.newInstance(destination);
}
}
getSupportFragmentManager().beginTransaction()
.add(R.id.container, f).commit();
}

View File

@@ -124,7 +124,7 @@ public class EditContactFragment extends EditPictureFragment {
}
private void initializeContact() {
String newName = getArguments().getString(NEW_NAME);
String newDest = getArguments().getString(NEW_DESTINATION);
if (mDestination != null) {
try {
@@ -141,9 +141,9 @@ public class EditContactFragment extends EditPictureFragment {
// TODO Handle
e.printStackTrace();
}
} else if (newName != null) {
mNameField.setText(newName);
mDestinationField.setText(getArguments().getString(NEW_DESTINATION));
} else if (newDest != null) {
mNameField.setText(getArguments().getString(NEW_NAME));
mDestinationField.setText(newDest);
}
}

View File

@@ -69,7 +69,7 @@ public class ViewIdentityFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_view_identity, container, false);
}
@@ -105,7 +105,7 @@ public class ViewIdentityFragment extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ActionBarActivity activity = ((ActionBarActivity)getActivity());
ActionBarActivity activity = ((ActionBarActivity) getActivity());
// Set the action bar
activity.setSupportActionBar(mToolbar);
@@ -148,7 +148,7 @@ public class ViewIdentityFragment extends Fragment {
@Override
public void onClick(View view) {
IntentIntegrator i = new IntentIntegrator(getActivity());
i.shareText("bote:" + mKey);
i.shareText(Constants.EMAILDEST_SCHEME + ":" + mKey);
}
});
@@ -164,49 +164,49 @@ public class ViewIdentityFragment extends Fragment {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit_identity:
Intent ei = new Intent(getActivity(), EditIdentityActivity.class);
ei.putExtra(EditIdentityFragment.IDENTITY_KEY, mKey);
startActivity(ei);
return true;
case R.id.action_edit_identity:
Intent ei = new Intent(getActivity(), EditIdentityActivity.class);
ei.putExtra(EditIdentityFragment.IDENTITY_KEY, mKey);
startActivity(ei);
return true;
case R.id.action_delete_identity:
DialogFragment df = new DialogFragment() {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.delete_identity)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
try {
BoteHelper.deleteIdentity(mKey);
getActivity().setResult(Activity.RESULT_OK);
getActivity().finish();
} catch (PasswordException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
case R.id.action_delete_identity:
DialogFragment df = new DialogFragment() {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.delete_identity)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
try {
BoteHelper.deleteIdentity(mKey);
getActivity().setResult(Activity.RESULT_OK);
getActivity().finish();
} catch (PasswordException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}
}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
};
df.show(getActivity().getSupportFragmentManager(), "deletecontact");
return true;
});
return builder.create();
}
};
df.show(getActivity().getSupportFragmentManager(), "deletecontact");
return true;
default:
return super.onOptionsItemSelected(item);
default:
return super.onOptionsItemSelected(item);
}
}
@@ -257,11 +257,12 @@ public class ViewIdentityFragment extends Fragment {
// render with minimal size
return QrCodeUtils.getQRCodeBitmap(qrCodeContent, 0);
}
protected void onPostExecute(Bitmap qrCode) {
// only change view, if fragment is attached to activity
// only change view, if fragment is attached to activity
if (ViewIdentityFragment.this.isAdded()) {
// scale the image up to our actual size. we do this in code rather
// than let the ImageView do this because we don't require filtering.
// scale the image up to our actual size. we do this in code rather
// than let the ImageView do this because we don't require filtering.
Bitmap scaled = Bitmap.createScaledBitmap(qrCode,
mKeyQrCode.getHeight(), mKeyQrCode.getHeight(),
false);

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 691 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@@ -23,6 +23,14 @@
app:fab_addButtonColorNormal="@color/accent"
app:fab_addButtonColorPressed="@color/accent_dark">
<net.i2p.android.ext.floatingactionbutton.FloatingActionButton
android:id="@+id/action_scan_qr_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_colorNormal="@color/accent"
app:fab_colorPressed="@color/accent_dark"
app:fab_icon="@drawable/ic_scan_qr_code_white_24dp"/>
<net.i2p.android.ext.floatingactionbutton.FloatingActionButton
android:id="@+id/action_new_contact"
android:layout_width="wrap_content"