diff --git a/ACardEmulator/app/src/main/java/com/vsmartcard/acardemulator/AppCompatPreferenceActivity.java b/ACardEmulator/app/src/main/java/com/vsmartcard/acardemulator/AppCompatPreferenceActivity.java new file mode 100644 index 0000000..870efc0 --- /dev/null +++ b/ACardEmulator/app/src/main/java/com/vsmartcard/acardemulator/AppCompatPreferenceActivity.java @@ -0,0 +1,109 @@ +package com.vsmartcard.remotesmartcardreader.app; + +import android.content.res.Configuration; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.support.annotation.LayoutRes; +import android.support.annotation.Nullable; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatDelegate; +import android.support.v7.widget.Toolbar; +import android.view.MenuInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls + * to be used with AppCompat. + */ +public abstract class AppCompatPreferenceActivity extends PreferenceActivity { + + private AppCompatDelegate mDelegate; + + @Override + protected void onCreate(Bundle savedInstanceState) { + getDelegate().installViewFactory(); + getDelegate().onCreate(savedInstanceState); + super.onCreate(savedInstanceState); + } + + @Override + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + getDelegate().onPostCreate(savedInstanceState); + } + + public ActionBar getSupportActionBar() { + return getDelegate().getSupportActionBar(); + } + + public void setSupportActionBar(@Nullable Toolbar toolbar) { + getDelegate().setSupportActionBar(toolbar); + } + + @Override + public MenuInflater getMenuInflater() { + return getDelegate().getMenuInflater(); + } + + @Override + public void setContentView(@LayoutRes int layoutResID) { + getDelegate().setContentView(layoutResID); + } + + @Override + public void setContentView(View view) { + getDelegate().setContentView(view); + } + + @Override + public void setContentView(View view, ViewGroup.LayoutParams params) { + getDelegate().setContentView(view, params); + } + + @Override + public void addContentView(View view, ViewGroup.LayoutParams params) { + getDelegate().addContentView(view, params); + } + + @Override + protected void onPostResume() { + super.onPostResume(); + getDelegate().onPostResume(); + } + + @Override + protected void onTitleChanged(CharSequence title, int color) { + super.onTitleChanged(title, color); + getDelegate().setTitle(title); + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + getDelegate().onConfigurationChanged(newConfig); + } + + @Override + protected void onStop() { + super.onStop(); + getDelegate().onStop(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + getDelegate().onDestroy(); + } + + public void invalidateOptionsMenu() { + getDelegate().invalidateOptionsMenu(); + } + + private AppCompatDelegate getDelegate() { + if (mDelegate == null) { + mDelegate = AppCompatDelegate.create(this, null); + } + return mDelegate; + } +} diff --git a/ACardEmulator/app/src/main/java/com/vsmartcard/acardemulator/SettingsActivity.java b/ACardEmulator/app/src/main/java/com/vsmartcard/acardemulator/SettingsActivity.java new file mode 100644 index 0000000..f2eba45 --- /dev/null +++ b/ACardEmulator/app/src/main/java/com/vsmartcard/acardemulator/SettingsActivity.java @@ -0,0 +1,247 @@ +/* + * Copyright (C) 2014 Frank Morgner + * + * This file is part of RemoteSmartCardReader. + * + * RemoteSmartCardReader is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * RemoteSmartCardReader is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * RemoteSmartCardReader. If not, see . + */ + +package com.vsmartcard.remotesmartcardreader.app; + + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.PreferenceActivity; +import android.provider.Settings; +import android.support.design.widget.Snackbar; +import android.support.v7.app.ActionBar; +import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; +import android.view.MenuItem; +import com.google.zxing.integration.android.IntentIntegrator; +import com.google.zxing.integration.android.IntentResult; + +/** + * A {@link PreferenceActivity} that presents a set of application settings. On + * handset devices, settings are presented as a single list. On tablets, + * settings are split by category, with category headers shown to the left of + * the list of settings. + *

+ * See + * Android Design: Settings for design guidelines and the Settings + * API Guide for more information on developing a Settings UI. + */ +public class SettingsActivity extends AppCompatPreferenceActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setupActionBar(); + + // Display the fragment as the main content. + getFragmentManager().beginTransaction().replace(android.R.id.content, + new VPCDPreferenceFragment()).commit(); + } + + /** + * Set up the {@link android.app.ActionBar}, if the API is available. + */ + private void setupActionBar() { + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + // Show the Up button in the action bar. + actionBar.setDisplayHomeAsUpEnabled(true); + } + } + + + /** {@inheritDoc} */ + @Override + public boolean onIsMultiPane() { + return isXLargeTablet(this); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int id = item.getItemId(); + if (id == android.R.id.home) { + finish(); + return true; + } + return super.onOptionsItemSelected(item); + } + + /** + * Helper method to determine if the device has an extra-large screen. For + * example, 10" tablets are extra-large. + */ + private static boolean isXLargeTablet(Context context) { + return (context.getResources().getConfiguration().screenLayout + & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE; + } + + /** + * A settings value change listener that updates the settings's summary + * to reflect its new value. + */ + private static final Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object value) { + String stringValue = value.toString(); + + if (preference instanceof ListPreference) { + // For list preferences, look up the correct display value in + // the settings's 'entries' list. + ListPreference listPreference = (ListPreference) preference; + int index = listPreference.findIndexOfValue(stringValue); + + // Set the summary to reflect the new value. + preference.setSummary( + index >= 0 + ? listPreference.getEntries()[index] + : null); + } else { + // For all other preferences, set the summary to the value's + // simple string representation. + preference.setSummary(stringValue); + } + return true; + } + }; + + /** + * Binds a settings's summary to its value. More specifically, when the + * settings's value is changed, its summary (line of text below the + * settings title) is updated to reflect the value. The summary is also + * immediately updated upon calling this method. The exact display format is + * dependent on the type of settings. + * + * @see #sBindPreferenceSummaryToValueListener + */ + private static void bindPreferenceSummaryToValue(Preference preference) { + // Set the listener to watch for value changes. + preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); + + // Trigger the listener immediately with the settings's + // current value. + sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, + PreferenceManager + .getDefaultSharedPreferences(preference.getContext()) + .getString(preference.getKey(), "")); + } + + /** + * This fragment shows data and sync preferences only. It is used when the + * activity is showing a two-pane settings UI. + */ + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static class VPCDPreferenceFragment extends PreferenceFragment { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.settings); + setHasOptionsMenu(true); + + // Bind the summaries of EditText/List/Dialog/Ringtone preferences + // to their values. When their values change, their summaries are + // updated to reflect the new value, per the Android Design + // guidelines. + bindPreferenceSummaryToValue(findPreference("hostname")); + bindPreferenceSummaryToValue(findPreference("port")); + bindPreferenceSummaryToValue(findPreference("delay")); + + Preference nfcSettings = findPreference("nfcSettings"); + nfcSettings.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + public boolean onPreferenceClick(Preference preference) { + Intent viewIntent = new Intent(Settings.ACTION_NFC_SETTINGS); + startActivity(viewIntent); + return true; + } + }); + + Preference scan = findPreference("scan"); + scan.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + public boolean onPreferenceClick(Preference preference) { + new IntentIntegrator(getActivity()).initiateScan(); + return true; + } + }); + } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int id = item.getItemId(); + if (id == android.R.id.home) { + startActivity(new Intent(getActivity(), SettingsActivity.class)); + return true; + } + return super.onOptionsItemSelected(item); + } + + } + + public void onActivityResult(int requestCode, int resultCode, Intent intent) { + IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); + switch(requestCode) { + case IntentIntegrator.REQUEST_CODE: + if (resultCode != RESULT_CANCELED) { + handleScannedURI(Uri.parse(scanResult.getContents())); + } + break; + } + } + + private void handleScannedURI(Uri uri) { + try { + String h, p; + h = uri.getHost(); + int _p = uri.getPort(); + if (_p < 0) { + _p = VPCDWorker.DEFAULT_PORT; + } + p = Integer.toString(_p); + SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(this); + SP.edit().putString("hostname", h).apply(); + SP.edit().putString("port", p).apply(); + } catch (Exception e) { + Snackbar.make(this.getCurrentFocus(), "Could not import configuration", Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + } + } + + @Override + public void onResume() { + super.onResume(); + Intent intent = getIntent(); + // Check to see that the Activity started due to a configuration URI + if (Intent.ACTION_VIEW.equals(intent.getAction())) { + Uri uri = intent.getData(); + handleScannedURI(uri); + super.onNewIntent(intent); + } + } + + @Override + public void onNewIntent(Intent intent) { + // onResume gets called after this to handle the intent + setIntent(intent); + } +} diff --git a/ACardEmulator/app/src/main/res/menu/main.xml b/ACardEmulator/app/src/main/res/menu/main.xml index 5e1be4d..1433375 100644 --- a/ACardEmulator/app/src/main/res/menu/main.xml +++ b/ACardEmulator/app/src/main/res/menu/main.xml @@ -13,6 +13,17 @@ android:icon="@drawable/ic_action_delete" app:showAsAction="withText" /> + + + + GIDS Applet (AID A000000397425446590201)\n \n Place your device on a contact-less reader. When the reader issues a SELECT command for one of the application identifiers above, the emulator creates the appropriate applet. The emulated applet will then handle all subsequent APDUs. + Settings diff --git a/ACardEmulator/app/src/main/res/xml/settings.xml b/ACardEmulator/app/src/main/res/xml/settings.xml new file mode 100644 index 0000000..421589a --- /dev/null +++ b/ACardEmulator/app/src/main/res/xml/settings.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + \ No newline at end of file