diff options
Diffstat (limited to 'vdrmanager/src')
5 files changed, 509 insertions, 398 deletions
diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/app/VdrManagerApp.java b/vdrmanager/src/de/bjusystems/vdrmanager/app/VdrManagerApp.java index 5f4a3de..fd301f0 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/app/VdrManagerApp.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/app/VdrManagerApp.java @@ -1,5 +1,6 @@ package de.bjusystems.vdrmanager.app; +import java.security.KeyStore; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -16,114 +17,143 @@ import de.bjusystems.vdrmanager.data.Vdr; public class VdrManagerApp extends Application { - public enum EpgListState { - EPG_TIME, EPG_CHANNEL, EPG_SEARCH - } - - private EpgListState epgListState; - private Event currentEvent; - private Timer currentTimer; - private Channel currentChannel; - - public static final Locale SYSTEM_LOCALE = Locale.getDefault(); - - private Vdr currentVDR; - - public Vdr getCurrentVDR() { - return currentVDR; - } - - public void setCurrentVDR(Vdr currentVDR) { - this.currentVDR = currentVDR; - } - - private List<Event> currentEpgList = new ArrayList<Event>(); - - public List<Event> getCurrentEpgList() { - return currentEpgList; - } - - public void setCurrentEpgList(List currentEpgList) { - this.currentEpgList = currentEpgList; - } - - private EpgSearchParams currentSearch; - private Class<? extends Activity> nextActivity; - private final List<Activity> activitiesToFinish = new ArrayList<Activity>(); - private boolean reload; - - @Override - public void onCreate() { - super.onCreate(); - Preferences.init(this); - } - - public void clear() { - this.currentEvent = null; - this.currentTimer = null; - this.currentChannel = null; - this.currentSearch = null; - this.currentEpgList = null; - this.epgListState = EpgListState.EPG_TIME; - } - - public Event getCurrentEvent() { - return currentEvent; - } - - public void setCurrentEvent(final Event currentEvent) { - this.currentEvent = currentEvent; - } - - public Timer getCurrentTimer() { - return currentTimer; - } - - public void setCurrentTimer(final Timer currentTimer) { - this.currentTimer = currentTimer; - } - - public Channel getCurrentChannel() { - return currentChannel; - } - - public void setCurrentChannel(final Channel currentChannel) { - clear(); - this.currentChannel = currentChannel; - this.epgListState = EpgListState.EPG_CHANNEL; - } - - public EpgSearchParams getCurrentSearch() { - return currentSearch; - } - - public void setCurrentSearch(final EpgSearchParams currentSearch) { - clear(); - this.currentSearch = currentSearch; - this.epgListState = EpgListState.EPG_SEARCH; - } - - public EpgListState getEpgListState() { - return epgListState; - } - - public Class<? extends Activity> getNextActivity() { - return nextActivity; - } - - public void setNextActivity(final Class<? extends Activity> nextActivity) { - this.nextActivity = nextActivity; - } - - public List<Activity> getActivitiesToFinish() { - return activitiesToFinish; - } - - public boolean isReload() { - return reload; - } - - public void setReload(final boolean reload) { - this.reload = reload; - } + + public VdrManagerApp() { + super(); + initSessionKeyStore(); + } + + public enum EpgListState { + EPG_TIME, EPG_CHANNEL, EPG_SEARCH + } + + private EpgListState epgListState; + private Event currentEvent; + private Timer currentTimer; + private Channel currentChannel; + + public static final Locale SYSTEM_LOCALE = Locale.getDefault(); + + private Vdr currentVDR; + + public Vdr getCurrentVDR() { + return currentVDR; + } + + public void setCurrentVDR(final Vdr currentVDR) { + this.currentVDR = currentVDR; + } + + private List<Event> currentEpgList = new ArrayList<Event>(); + + public List<Event> getCurrentEpgList() { + return currentEpgList; + } + + public void setCurrentEpgList(final List currentEpgList) { + this.currentEpgList = currentEpgList; + } + + private EpgSearchParams currentSearch; + private Class<? extends Activity> nextActivity; + private final List<Activity> activitiesToFinish = new ArrayList<Activity>(); + private boolean reload; + + @Override + public void onCreate() { + super.onCreate(); + Preferences.init(this); + } + + public void clear() { + this.currentEvent = null; + this.currentTimer = null; + this.currentChannel = null; + this.currentSearch = null; + this.currentEpgList = null; + this.epgListState = EpgListState.EPG_TIME; + } + + public Event getCurrentEvent() { + return currentEvent; + } + + public void setCurrentEvent(final Event currentEvent) { + this.currentEvent = currentEvent; + } + + public Timer getCurrentTimer() { + return currentTimer; + } + + public void setCurrentTimer(final Timer currentTimer) { + this.currentTimer = currentTimer; + } + + public Channel getCurrentChannel() { + return currentChannel; + } + + public void setCurrentChannel(final Channel currentChannel) { + clear(); + this.currentChannel = currentChannel; + this.epgListState = EpgListState.EPG_CHANNEL; + } + + public EpgSearchParams getCurrentSearch() { + return currentSearch; + } + + public void setCurrentSearch(final EpgSearchParams currentSearch) { + clear(); + this.currentSearch = currentSearch; + this.epgListState = EpgListState.EPG_SEARCH; + } + + public EpgListState getEpgListState() { + return epgListState; + } + + public Class<? extends Activity> getNextActivity() { + return nextActivity; + } + + public void setNextActivity(final Class<? extends Activity> nextActivity) { + this.nextActivity = nextActivity; + } + + public List<Activity> getActivitiesToFinish() { + return activitiesToFinish; + } + + public boolean isReload() { + return reload; + } + + public void setReload(final boolean reload) { + this.reload = reload; + } + + /** KeyStore for per app run accepted certificates */ + private KeyStore sessionKeyStore; + + /** + * Gets the temporary accepted certificates + * @return KeyStore + */ + public KeyStore getSessionKeyStore() { + return sessionKeyStore; + } + + /** + * Create a new and empty key store + */ + public void initSessionKeyStore() { + try { + sessionKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + sessionKeyStore.load(null); + } catch (final Exception e) { + sessionKeyStore = null; + } + } } diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/gui/CertificateProblemDialog.java b/vdrmanager/src/de/bjusystems/vdrmanager/gui/CertificateProblemDialog.java index 3e3d6bf..bf7034f 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/gui/CertificateProblemDialog.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/gui/CertificateProblemDialog.java @@ -90,4 +90,9 @@ public class CertificateProblemDialog implements CertificateProblemListener { return action; } + + @Override + public Activity getCurrentActivity() { + return activity; + } } diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrManagerActivity.java b/vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrManagerActivity.java index 7c5da2c..27163e0 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrManagerActivity.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrManagerActivity.java @@ -29,282 +29,295 @@ import de.bjusystems.vdrmanager.data.db.EPGSearchSuggestionsProvider; import de.bjusystems.vdrmanager.utils.wakeup.AsyncWakeupTask; public class VdrManagerActivity extends SherlockActivity implements - OnClickListener, OnQueryTextListener { - - public static final String TAG = "VdrManagerActivity"; - - public static final String VDR_PORTAL = "http://www.vdr-portal.de"; - - private com.actionbarsherlock.widget.SearchView search; - - private View actionMenuWakup; - - @Override - protected void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Preferences.initVDR(this); - - // if(Preferences.get().getCurrentVdr() == null){ - // finish(); - // return; - // } - - if (Preferences.initVDR(this) == false) { - Intent intent = new Intent(); - intent.setClass(this, VdrListActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra(Intents.EMPTY_CONFIG, Boolean.TRUE); - startActivity(intent); - Toast.makeText(this, R.string.no_vdr, Toast.LENGTH_SHORT).show(); - finish(); - return; - } - - Preferences.setLocale(this); - - // this.getActionBar().setDisplayShowCustomEnabled(true); - // this.getActionBar().setDisplayShowTitleEnabled(false); - // setTitle(getString(R.string.app_name)); - // attach view - setContentView(R.layout.vdrmanager); - - // Preferences.loadPreferences(this); - - findViewById(R.id.action_menu_channels).setOnClickListener(this); - findViewById(R.id.action_menu_recordings).setOnClickListener(this); - findViewById(R.id.action_menu_timers).setOnClickListener(this); - findViewById(R.id.action_menu_epg).setOnClickListener(this); - View v = findViewById(R.id.action_menu_search); - if (v != null) { - v.setOnClickListener(this); - } - findViewById(R.id.main_logo).setOnClickListener(this); - actionMenuWakup = findViewById(R.id.action_menu_wakeup); - - // add and register buttons - // createButtons(); - } - - public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { - com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater(); - inflater.inflate(R.menu.main_menu, menu); - - // search = new SearchView(getSupportActionBar().getThemedContext()); - - search = (SearchView) menu.findItem(R.id.menu_search).getActionView(); - - // search = (SearchView) - // .getActionView(); - // - // Object o = menu.findItem(R.id.menu_search); - - SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); - search.setSearchableInfo(searchManager - .getSearchableInfo(getComponentName())); - - // search.setOnQueryTextListener(this); - return true; - } - - @Override - protected void onResume() { - Preferences.setLocale(this); - if (Preferences.get().isWakeupEnabled() == false) { - actionMenuWakup.setVisibility(View.GONE); - actionMenuWakup.setOnClickListener(null); - } else { - actionMenuWakup.setVisibility(View.VISIBLE); - actionMenuWakup.setOnClickListener(this); - } - super.onResume(); - } - - @Override - public boolean onOptionsItemSelected( - final com.actionbarsherlock.view.MenuItem item) { - - switch (item.getItemId()) { - case R.id.main_menu_preferences: { - Intent intent = new Intent(this, PreferencesActivity.class); - int flags = Intent.FLAG_ACTIVITY_NEW_TASK - | Intent.FLAG_ACTIVITY_SINGLE_TOP - | Intent.FLAG_ACTIVITY_CLEAR_TOP; - intent.setFlags(flags); - startActivity(intent); - finish(); - break; - } - case R.id.main_menu_info: { - if(!isFinishing()){ - return true; - } - About.show(this); - break; - } - case R.id.main_menu_exit: { - finish(); - break; - } - - case R.id.main_menu_clear_search: { - SearchRecentSuggestions suggestions = new SearchRecentSuggestions( - this, EPGSearchSuggestionsProvider.AUTHORITY, - EPGSearchSuggestionsProvider.MODE); - suggestions.clearHistory(); - break; - } - - // case R.id.menu_search: { - // if(Build.VERSION.SDK_INT <11){ - // onSearchRequested(); - // } - // break; - // } - case R.id.main_menu_goto: { - try { - final Cursor cursor = ((AndroidDatabaseResults) DBAccess - .get(this).getVdrDAO().iterator().getRawResults()) - .getRawCursor(); - startManagingCursor(cursor); - final AlertDialog ad = new AlertDialog.Builder(this) - .setSingleChoiceItems(cursor, findVdrCursor(cursor), - "name", new DialogInterface.OnClickListener() { - - public void onClick(DialogInterface dialog, - int which) { - cursor.moveToPosition(which); - int id = cursor.getInt(cursor - .getColumnIndex("_id")); - Vdr vdr = DBAccess - .get(VdrManagerActivity.this) - .getVdrDAO().queryForId(id); - if (vdr == null) { - Toast.makeText( - VdrManagerActivity.this, - R.string.main_menu_goto_no_vdr, - Toast.LENGTH_SHORT).show(); - } else { - Preferences.setCurrentVdr( - VdrManagerActivity.this, - vdr); - Toast.makeText( - VdrManagerActivity.this, - getString( - R.string.main_menu_switched_to, - vdr.getName()), - Toast.LENGTH_SHORT).show(); - Intent intent = getIntent(); - overridePendingTransition(0, 0); - intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); - finish(); - - overridePendingTransition(0, 0); - startActivity(intent); - } - dialog.dismiss(); - } - })// - .setTitle(R.string.main_menu_goto_title)// - .create(); - ad.show(); - - } catch (Exception ex) { - Log.w(TAG, ex); - } - - break; - } - } - return true; - } - - private int findVdrCursor(Cursor c) { - if (Preferences.get().getCurrentVdr() == null) { - return -1; - } - - int cid = Preferences.get().getCurrentVdr().getId(); - - int position = 0; - c.moveToPosition(-1); - while (c.moveToNext()) { - if (c.getInt(c.getColumnIndex("_id")) == cid) { - break; - } - position++; - } - return position; - } - - @Override - public void onBackPressed() { - if (Preferences.get().isQuiteOnBackButton()) { - super.onBackPressed(); - } - } - - public void startActivity(Class<?> clazz) { - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setClass(this, clazz); - startActivity(intent); - } - - public void onClick(View v) { - int id = v.getId(); - - switch (id) { - case R.id.action_menu_channels: - startActivity(ChannelListActivity.class); - break; - case R.id.action_menu_recordings: - startActivity(RecordingListActivity.class); - break; - case R.id.action_menu_timers: - startActivity(TimerListActivity.class); - break; - case R.id.action_menu_epg: - startActivity(TimeEpgListActivity.class); - break; - case R.id.action_menu_search: - onSearchRequested(); - break; - case R.id.action_menu_wakeup: - final AsyncWakeupTask wakeupTask = new AsyncWakeupTask(this); - wakeupTask.execute(); - break; - case R.id.main_logo: - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse(VDR_PORTAL)); - startActivity(i); - break; - } - - } - - protected void startSearchManager() { - Bundle appData = new Bundle(); - startSearch(null, false, appData, false); - } - - @Override - public boolean onSearchRequested() { - search.setVisibility(View.VISIBLE); - // Bundle appData = new Bundle(); - // appData.putBoolean(SearchableActivity.JARGON, true); - // startSearch(null, false, appData, false); - return true; - } - - @Override - public boolean onQueryTextSubmit(String query) { - // TODO Auto-generated method stub - return false; - } - - @Override - public boolean onQueryTextChange(String newText) { - // TODO Auto-generated method stub - return false; - } +OnClickListener, OnQueryTextListener { + + public static final String TAG = "VdrManagerActivity"; + + public static final String VDR_PORTAL = "http://www.vdr-portal.de"; + + private com.actionbarsherlock.widget.SearchView search; + + private View actionMenuWakup; + + @Override + protected void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Preferences.initVDR(this); + + // if(Preferences.get().getCurrentVdr() == null){ + // finish(); + // return; + // } + + if (Preferences.initVDR(this) == false) { + final Intent intent = new Intent(); + intent.setClass(this, VdrListActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.putExtra(Intents.EMPTY_CONFIG, Boolean.TRUE); + startActivity(intent); + Toast.makeText(this, R.string.no_vdr, Toast.LENGTH_SHORT).show(); + finish(); + return; + } + + Preferences.setLocale(this); + + // this.getActionBar().setDisplayShowCustomEnabled(true); + // this.getActionBar().setDisplayShowTitleEnabled(false); + // setTitle(getString(R.string.app_name)); + // attach view + setContentView(R.layout.vdrmanager); + + // Preferences.loadPreferences(this); + + findViewById(R.id.action_menu_channels).setOnClickListener(this); + findViewById(R.id.action_menu_recordings).setOnClickListener(this); + findViewById(R.id.action_menu_timers).setOnClickListener(this); + findViewById(R.id.action_menu_epg).setOnClickListener(this); + final View v = findViewById(R.id.action_menu_search); + if (v != null) { + v.setOnClickListener(this); + } + findViewById(R.id.main_logo).setOnClickListener(this); + actionMenuWakup = findViewById(R.id.action_menu_wakeup); + + // add and register buttons + // createButtons(); + } + + @Override + public boolean onCreateOptionsMenu(final com.actionbarsherlock.view.Menu menu) { + final com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater(); + inflater.inflate(R.menu.main_menu, menu); + + // search = new SearchView(getSupportActionBar().getThemedContext()); + + search = (SearchView) menu.findItem(R.id.menu_search).getActionView(); + + // search = (SearchView) + // .getActionView(); + // + // Object o = menu.findItem(R.id.menu_search); + + final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); + search.setSearchableInfo(searchManager + .getSearchableInfo(getComponentName())); + + // search.setOnQueryTextListener(this); + return true; + } + + @Override + protected void onResume() { + Preferences.setLocale(this); + if (Preferences.get().isWakeupEnabled() == false) { + actionMenuWakup.setVisibility(View.GONE); + actionMenuWakup.setOnClickListener(null); + } else { + actionMenuWakup.setVisibility(View.VISIBLE); + actionMenuWakup.setOnClickListener(this); + } + super.onResume(); + } + + @Override + public boolean onOptionsItemSelected( + final com.actionbarsherlock.view.MenuItem item) { + + switch (item.getItemId()) { + case R.id.main_menu_preferences: { + final Intent intent = new Intent(this, PreferencesActivity.class); + final int flags = Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_SINGLE_TOP + | Intent.FLAG_ACTIVITY_CLEAR_TOP; + intent.setFlags(flags); + startActivity(intent); + finish(); + break; + } + case R.id.main_menu_info: { + if(!isFinishing()){ + return true; + } + About.show(this); + break; + } + case R.id.main_menu_exit: { + finish(); + break; + } + + case R.id.main_menu_clear_search: { + final SearchRecentSuggestions suggestions = new SearchRecentSuggestions( + this, EPGSearchSuggestionsProvider.AUTHORITY, + EPGSearchSuggestionsProvider.MODE); + suggestions.clearHistory(); + break; + } + + // case R.id.menu_search: { + // if(Build.VERSION.SDK_INT <11){ + // onSearchRequested(); + // } + // break; + // } + case R.id.main_menu_goto: { + try { + final Cursor cursor = ((AndroidDatabaseResults) DBAccess + .get(this).getVdrDAO().iterator().getRawResults()) + .getRawCursor(); + startManagingCursor(cursor); + final AlertDialog ad = new AlertDialog.Builder(this) + .setSingleChoiceItems(cursor, findVdrCursor(cursor), + "name", new DialogInterface.OnClickListener() { + + @Override + public void onClick(final DialogInterface dialog, + final int which) { + cursor.moveToPosition(which); + final int id = cursor.getInt(cursor + .getColumnIndex("_id")); + final Vdr vdr = DBAccess + .get(VdrManagerActivity.this) + .getVdrDAO().queryForId(id); + if (vdr == null) { + Toast.makeText( + VdrManagerActivity.this, + R.string.main_menu_goto_no_vdr, + Toast.LENGTH_SHORT).show(); + } else { + Preferences.setCurrentVdr( + VdrManagerActivity.this, + vdr); + Toast.makeText( + VdrManagerActivity.this, + getString( + R.string.main_menu_switched_to, + vdr.getName()), + Toast.LENGTH_SHORT).show(); + final Intent intent = getIntent(); + overridePendingTransition(0, 0); + intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + finish(); + + overridePendingTransition(0, 0); + startActivity(intent); + } + dialog.dismiss(); + } + })// + .setTitle(R.string.main_menu_goto_title)// + .create(); + ad.show(); + + } catch (final Exception ex) { + Log.w(TAG, ex); + } + + break; + } + } + return true; + } + + private int findVdrCursor(final Cursor c) { + if (Preferences.get().getCurrentVdr() == null) { + return -1; + } + + final int cid = Preferences.get().getCurrentVdr().getId(); + + int position = 0; + c.moveToPosition(-1); + while (c.moveToNext()) { + if (c.getInt(c.getColumnIndex("_id")) == cid) { + break; + } + position++; + } + return position; + } + + @Override + public void onBackPressed() { + if (Preferences.get().isQuiteOnBackButton()) { + finish(); + } else { + super.onBackPressed(); + } + + try { + // reassign a new and empty key store + ((VdrManagerApp)getApplication()).initSessionKeyStore(); + } catch (final Exception e) { + Log.e(getClass().getName(), "Can't clear session key store"); + } + + } + + public void startActivity(final Class<?> clazz) { + final Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setClass(this, clazz); + startActivity(intent); + } + + @Override + public void onClick(final View v) { + final int id = v.getId(); + + switch (id) { + case R.id.action_menu_channels: + startActivity(ChannelListActivity.class); + break; + case R.id.action_menu_recordings: + startActivity(RecordingListActivity.class); + break; + case R.id.action_menu_timers: + startActivity(TimerListActivity.class); + break; + case R.id.action_menu_epg: + startActivity(TimeEpgListActivity.class); + break; + case R.id.action_menu_search: + onSearchRequested(); + break; + case R.id.action_menu_wakeup: + final AsyncWakeupTask wakeupTask = new AsyncWakeupTask(this); + wakeupTask.execute(); + break; + case R.id.main_logo: + final Intent i = new Intent(Intent.ACTION_VIEW); + i.setData(Uri.parse(VDR_PORTAL)); + startActivity(i); + break; + } + + } + + protected void startSearchManager() { + final Bundle appData = new Bundle(); + startSearch(null, false, appData, false); + } + + @Override + public boolean onSearchRequested() { + search.setVisibility(View.VISIBLE); + // Bundle appData = new Bundle(); + // appData.putBoolean(SearchableActivity.JARGON, true); + // startSearch(null, false, appData, false); + return true; + } + + @Override + public boolean onQueryTextSubmit(final String query) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean onQueryTextChange(final String newText) { + // TODO Auto-generated method stub + return false; + } } diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/utils/svdrp/CertificateProblemListener.java b/vdrmanager/src/de/bjusystems/vdrmanager/utils/svdrp/CertificateProblemListener.java index 4af370e..ac0ac1e 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/utils/svdrp/CertificateProblemListener.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/utils/svdrp/CertificateProblemListener.java @@ -2,6 +2,8 @@ package de.bjusystems.vdrmanager.utils.svdrp; import java.security.cert.X509Certificate; +import android.app.Activity; + /** * Interface for reporting problems with the SSL certificate * @author bju @@ -28,4 +30,10 @@ public interface CertificateProblemListener { * @param authType authentication type */ CertificateProblemAction reportProblem(final X509Certificate[] chain, final String authType); + + /** + * Gets the current activity + * @return activity + */ + Activity getCurrentActivity(); } diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/utils/svdrp/MySSLSocketFactory.java b/vdrmanager/src/de/bjusystems/vdrmanager/utils/svdrp/MySSLSocketFactory.java index 3f1f2be..6c87faa 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/utils/svdrp/MySSLSocketFactory.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/utils/svdrp/MySSLSocketFactory.java @@ -1,6 +1,9 @@ package de.bjusystems.vdrmanager.utils.svdrp; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.net.Socket; import java.security.KeyManagementException; import java.security.KeyStore; @@ -19,6 +22,11 @@ import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; +import android.app.Activity; +import android.content.Context; +import android.util.Log; +import de.bjusystems.vdrmanager.app.VdrManagerApp; + /** * SSLSocketFactory @@ -26,8 +34,11 @@ import javax.net.ssl.X509TrustManager; */ public class MySSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactory { + /** The key store file */ + private final String keyStoreFile = "KeyStore"; + /** the key store */ - private KeyStore keyStore; + private KeyStore appKeyStore; /** the real socket factory */ private final SSLSocketFactory sslFactory; @@ -35,18 +46,24 @@ public class MySSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactor /** the trust managers */ private X509TrustManager[] trustManagers; + /** the current activity */ + private final Activity activity; + public MySSLSocketFactory(final boolean acceptAllCertificates, final CertificateProblemListener certProblemListener) throws KeyManagementException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException { super(null); + // save context + this.activity = certProblemListener.getCurrentActivity(); + // accept all host names this.setHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - // load the keystore + // load the key store initKeyStore(); - // init the trust managers + // initialize the trust managers if (acceptAllCertificates) { initInsecureTrustManagers(); } else { @@ -74,8 +91,18 @@ public class MySSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactor */ private void initKeyStore() throws KeyStoreException { - keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); - // keyStore.load(...); + try { + appKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + + try { + final InputStream stream = activity.openFileInput(keyStoreFile); + appKeyStore.load(stream, null); + } catch (final FileNotFoundException e) { + appKeyStore.load(null); + } + } catch (final Exception e) { + throw new KeyStoreException(e); + } } /** @@ -89,15 +116,23 @@ public class MySSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactor final List<X509TrustManager> trustManagerList = new ArrayList<X509TrustManager>(); - // init the trust manager accepting certificates contained in the key store + // initialize the trust manager accepting certificates contained in the session key store TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509"); - trustManagerFactory.init(keyStore); + trustManagerFactory.init(((VdrManagerApp)activity.getApplication()).getSessionKeyStore()); X509TrustManager trustManager = getTrustManager(trustManagerFactory); if (trustManager != null) { trustManagerList.add(trustManager); } - // init the trust manager accepting certificates accepted from the system + // initialize the trust manager accepting certificates contained in the permanent key store + trustManagerFactory = TrustManagerFactory.getInstance("X509"); + trustManagerFactory.init(appKeyStore); + trustManager = getTrustManager(trustManagerFactory); + if (trustManager != null) { + trustManagerList.add(trustManager); + } + + // initialize the trust manager accepting certificates accepted from the system trustManagerFactory = TrustManagerFactory.getInstance("X509"); trustManagerFactory.init((KeyStore)null); trustManager = getTrustManager(trustManagerFactory); @@ -127,9 +162,10 @@ public class MySSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactor switch (certProblemListener.reportProblem(chain, authType)) { case ACCEPT_ONCE: + saveCertificate(chain, authType, false); return; case ACCEPT_FOREVER: - saveCertificate(chain, authType); + saveCertificate(chain, authType, true); return; default: if (lastException != null) { @@ -193,6 +229,25 @@ public class MySSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactor * @param chain certificate chain * @param authType authentication type */ - private void saveCertificate(final X509Certificate[] chain, final String authType) { + private void saveCertificate(final X509Certificate[] chain, final String authType, final boolean permanently) { + + // get the certificate alias + final String alias = chain[0].getSubjectDN().toString(); + + // key store to use + final KeyStore saveKeyStore = permanently ? appKeyStore : ((VdrManagerApp)activity.getApplication()).getSessionKeyStore(); + + // store the certificate for this alias + try { + saveKeyStore.setCertificateEntry(alias, chain[0]); + + // the session key store is not saved + if (permanently) { + final FileOutputStream stream = activity.openFileOutput(keyStoreFile, Context.MODE_PRIVATE); + saveKeyStore.store(stream, null); + } + } catch (final Exception e) { + Log.e(getClass().getName(), "Can't save certificate for ' " + alias + "' as trusted"); + } } } |