diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index 70fb3b3d6..e829df7a0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -123,72 +123,13 @@ public class ImportKeysActivity extends ActionBarActivity { mNavigationStrings = getResources().getStringArray(R.array.import_action_list); - if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN.equals(getIntent().getAction())) { - setTitle(R.string.nav_import); - } else { - initTabs(); - } + // TODO: add actionbar button for this action? +// if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN.equals(getIntent().getAction())) { +// } handleActions(savedInstanceState, getIntent()); } - private void initTabs() { - mTabsAdapter = new PagerTabStripAdapter(this); - mViewPager.setAdapter(mTabsAdapter); - mSlidingTabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { - @Override - public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { - // resize view pager back to 64 if keyserver settings have been collapsed - if (getViewPagerHeight() > VIEW_PAGER_HEIGHT) { - resizeViewPager(VIEW_PAGER_HEIGHT); - } - } - - @Override - public void onPageSelected(int position) { - } - - @Override - public void onPageScrollStateChanged(int state) { - } - }); - - Bundle serverBundle = new Bundle(); -// serverBundle.putParcelable(ViewKeyMainFragment.ARG_DATA_URI, dataUri); - mTabsAdapter.addTab(ImportKeysServerFragment.class, - serverBundle, getString(R.string.import_tab_keyserver)); - - Bundle qrCodeBundle = new Bundle(); -// importBundle.putParcelable(ViewKeyMainFragment.ARG_DATA_URI, dataUri); - mTabsAdapter.addTab(ImportKeysQrCodeFragment.class, - qrCodeBundle, getString(R.string.import_tab_qr_code)); - - Bundle fileBundle = new Bundle(); -// importBundle.putParcelable(ViewKeyMainFragment.ARG_DATA_URI, dataUri); - mTabsAdapter.addTab(ImportKeysFileFragment.class, - fileBundle, getString(R.string.import_tab_direct)); - - Bundle keybaseBundle = new Bundle(); -// keybaseBundle.putParcelable(ViewKeyMainFragment.ARG_DATA_URI, dataUri); - mTabsAdapter.addTab(ImportKeysKeybaseFragment.class, - keybaseBundle, getString(R.string.import_tab_keybase)); - - // update layout after operations - mSlidingTabLayout.setViewPager(mViewPager); - - } - - @Override - public boolean onTouchEvent(MotionEvent event) { - boolean result = super.onTouchEvent(event); - - if (!result) { - mViewPager.onTouchEvent(event); - } - - return result; - } - protected void handleActions(Bundle savedInstanceState, Intent intent) { String action = intent.getAction(); Bundle extras = intent.getExtras(); @@ -205,6 +146,8 @@ public class ImportKeysActivity extends ActionBarActivity { action = ACTION_IMPORT_KEY; } + Bundle serverBundle = null; + boolean serverOnly = false; if (scheme != null && scheme.toLowerCase(Locale.ENGLISH).equals(Constants.FINGERPRINT_SCHEME)) { /* Scanning a fingerprint directly with Barcode Scanner */ loadFromFingerprintUri(savedInstanceState, dataUri); @@ -245,10 +188,8 @@ public class ImportKeysActivity extends ActionBarActivity { if (query != null && query.length() > 0) { // display keyserver fragment with query - Bundle args = new Bundle(); - args.putString(ImportKeysServerFragment.ARG_QUERY, query); -// loadNavFragment(NAV_SERVER, args); - //TODO: load afterwards! + serverBundle = new Bundle(); + serverBundle.putString(ImportKeysServerFragment.ARG_QUERY, query); mSwitchToTab = NAV_SERVER; // action: search immediately @@ -264,7 +205,19 @@ public class ImportKeysActivity extends ActionBarActivity { */ String fingerprint = intent.getStringExtra(EXTRA_FINGERPRINT); - loadFromFingerprint(savedInstanceState, fingerprint); + if (isFingerprintValid(fingerprint)) { + String query = "0x" + fingerprint; + + // display keyserver fragment with query + serverBundle = new Bundle(); + serverBundle.putString(ImportKeysServerFragment.ARG_QUERY, query); + serverBundle.putBoolean(ImportKeysServerFragment.ARG_DISABLE_QUERY_EDIT, true); + serverOnly = true; + mSwitchToTab = NAV_SERVER; + + // action: search immediately + startListFragment(savedInstanceState, null, null, query); + } } else { Log.e(Constants.TAG, "IMPORT_KEY_FROM_KEYSERVER action needs to contain the 'query', 'key_id', or " + @@ -303,6 +256,44 @@ public class ImportKeysActivity extends ActionBarActivity { startListFragment(savedInstanceState, null, null, null); } + initTabs(serverBundle, serverOnly); + } + + private void initTabs(Bundle serverBundle, boolean serverOnly) { + mTabsAdapter = new PagerTabStripAdapter(this); + mViewPager.setAdapter(mTabsAdapter); + mSlidingTabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + // resize view pager back to 64 if keyserver settings have been collapsed + if (getViewPagerHeight() > VIEW_PAGER_HEIGHT) { + resizeViewPager(VIEW_PAGER_HEIGHT); + } + } + + @Override + public void onPageSelected(int position) { + } + + @Override + public void onPageScrollStateChanged(int state) { + } + }); + + mTabsAdapter.addTab(ImportKeysServerFragment.class, + serverBundle, getString(R.string.import_tab_keyserver)); + if (!serverOnly) { + mTabsAdapter.addTab(ImportKeysQrCodeFragment.class, + null, getString(R.string.import_tab_qr_code)); + mTabsAdapter.addTab(ImportKeysFileFragment.class, + null, getString(R.string.import_tab_direct)); + mTabsAdapter.addTab(ImportKeysKeybaseFragment.class, + null, getString(R.string.import_tab_keybase)); + } + + // update layout after operations + mSlidingTabLayout.setViewPager(mViewPager); + mViewPager.setCurrentItem(mSwitchToTab); } @@ -343,31 +334,48 @@ public class ImportKeysActivity extends ActionBarActivity { Log.d(Constants.TAG, "fingerprint: " + fingerprint); - loadFromFingerprint(savedInstanceState, fingerprint); + // TODO: reload fragment when coming from qr code! +// loadFromFingerprint(savedInstanceState, fingerprint); + + +// String query = "0x" + fingerprint; +// +// // display keyserver fragment with query +// Bundle serverBundle = new Bundle(); +// serverBundle.putString(ImportKeysServerFragment.ARG_QUERY, query); +// serverBundle.putBoolean(ImportKeysServerFragment.ARG_DISABLE_QUERY_EDIT, true); +// +// return serverBundle; } - public void loadFromFingerprint(Bundle savedInstanceState, String fingerprint) { + private boolean isFingerprintValid(String fingerprint) { if (fingerprint == null || fingerprint.length() < 40) { SuperCardToast toast = SuperCardToast.create(this, getString(R.string.import_qr_code_too_short_fingerprint), SuperToast.Duration.LONG); toast.setBackground(SuperToast.Background.RED); toast.show(); - return; + return false; + } else { + return true; + } + } + + /** + * Scroll ViewPager left and right + * + * @param event + * @return + */ + @Override + public boolean onTouchEvent(MotionEvent event) { + boolean result = super.onTouchEvent(event); + + if (!result) { + mViewPager.onTouchEvent(event); } - String query = "0x" + fingerprint; - - // display keyserver fragment with query - Bundle args = new Bundle(); - args.putString(ImportKeysServerFragment.ARG_QUERY, query); - args.putBoolean(ImportKeysServerFragment.ARG_DISABLE_QUERY_EDIT, true); -// loadNavFragment(NAV_SERVER, args); - - //TODO - - // action: search directly - startListFragment(savedInstanceState, null, null, query); + return result; } public void loadCallback(ImportKeysListFragment.LoaderState loaderState) {