From 8c4b169d3eb2c50c198c6ec73b625f96571d42f2 Mon Sep 17 00:00:00 2001 From: Rashiq Ahmad Date: Wed, 4 Dec 2013 15:22:37 +0100 Subject: [PATCH] Added InputManager and fixed a NPE for the Tabs. --- .../kiwixmobile/KiwixMobileActivity.java | 12 +- .../kiwixmobile/KiwixMobileFragment.java | 171 ++++++++++-------- 2 files changed, 103 insertions(+), 80 deletions(-) diff --git a/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java b/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java index 36d1431ee..3a84f66e6 100644 --- a/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java +++ b/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java @@ -2,6 +2,8 @@ package org.kiwix.kiwixmobile; import android.app.ActionBar; import android.app.FragmentTransaction; +import android.content.ClipData; +import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; @@ -23,6 +25,7 @@ import android.view.ViewGroup; import android.view.ViewParent; import android.view.Window; import android.view.WindowManager; +import android.view.inputmethod.InputMethodManager; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.Spinner; @@ -126,10 +129,13 @@ public class KiwixMobileActivity extends FragmentActivity implements ActionBar.T mCurrentFragment = getCurrentVisibleFragment(); + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + switch (item.getItemId()) { case R.id.menu_home: case android.R.id.home: + imm.hideSoftInputFromWindow(mCurrentFragment.articleSearchtextView.getWindowToken(), 0); mCurrentFragment.openMainPage(); break; @@ -146,12 +152,14 @@ public class KiwixMobileActivity extends FragmentActivity implements ActionBar.T break; case R.id.menu_forward: + imm.hideSoftInputFromWindow(mCurrentFragment.articleSearchtextView.getWindowToken(), 0); if (mCurrentFragment.webView.canGoForward()) { mCurrentFragment.webView.goForward(); } break; case R.id.menu_back: + imm.hideSoftInputFromWindow(mCurrentFragment.articleSearchtextView.getWindowToken(), 0); if (mCurrentFragment.webView.canGoBack()) { mCurrentFragment.menu.findItem(R.id.menu_forward).setVisible(true); mCurrentFragment.webView.goBack(); @@ -466,7 +474,9 @@ public class KiwixMobileActivity extends FragmentActivity implements ActionBar.T getCurrentVisibleFragment().handleTabDeleteCross(); - v.startDrag(null, shadowBuilder, v, 0); + ClipData data = ClipData.newPlainText("", ""); + + v.startDrag(data, shadowBuilder, v, 0); return true; } diff --git a/src/org/kiwix/kiwixmobile/KiwixMobileFragment.java b/src/org/kiwix/kiwixmobile/KiwixMobileFragment.java index 3c28afe8d..6cdff0bf4 100644 --- a/src/org/kiwix/kiwixmobile/KiwixMobileFragment.java +++ b/src/org/kiwix/kiwixmobile/KiwixMobileFragment.java @@ -13,8 +13,8 @@ import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.net.Uri; -import android.os.Environment; import android.os.Bundle; +import android.os.Environment; import android.os.Handler; import android.os.Looper; import android.os.Message; @@ -25,27 +25,27 @@ import android.text.InputType; import android.text.TextWatcher; import android.util.DisplayMetrics; import android.util.Log; +import android.view.ContextMenu; import android.view.DragEvent; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.MenuItem.OnMenuItemClickListener; import android.view.MotionEvent; import android.view.View; -import android.view.MenuItem; import android.view.View.MeasureSpec; -import android.view.View.OnTouchListener; -import android.view.ContextMenu; import android.view.View.OnCreateContextMenuListener; +import android.view.View.OnTouchListener; import android.view.ViewGroup; -import android.view.MenuItem.OnMenuItemClickListener; import android.view.animation.AnimationUtils; import android.view.inputmethod.InputMethodManager; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; -import android.webkit.WebViewClient; import android.webkit.WebView.HitTestResult; +import android.webkit.WebViewClient; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; @@ -60,13 +60,11 @@ import android.widget.TextView.OnEditorActionListener; import android.widget.Toast; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.io.FileInputStream; import java.io.OutputStream; -import java.io.InputStream; -import java.io.FileOutputStream; +import java.util.ArrayList; public class KiwixMobileFragment extends Fragment { @@ -106,6 +104,8 @@ public class KiwixMobileFragment extends Fragment { public ImageButton exitFullscreenButton; + public AutoCompleteTextView articleSearchtextView; + protected boolean requestClearHistoryAfterLoad; protected boolean requestInitAllMenuItems; @@ -118,8 +118,6 @@ public class KiwixMobileFragment extends Fragment { private boolean isButtonEnabled = true; - private AutoCompleteTextView articleSearchtextView; - private ArrayAdapter adapter; private Button mBackToTopButton; @@ -310,6 +308,9 @@ public class KiwixMobileFragment extends Fragment { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { + InputMethodManager imm = (InputMethodManager) getActivity().getSystemService( + Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(articleSearchtextView.getWindowToken(), 0); articleSearchtextView.setText(parent.getItemAtPosition(position).toString()); openArticleFromSearch(); } @@ -325,79 +326,91 @@ public class KiwixMobileFragment extends Fragment { articleSearchtextView.setInputType(InputType.TYPE_CLASS_TEXT); - final Handler saveHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - String url = (String) msg.getData().get("url"); - String src = (String) msg.getData().get("src"); - - if (url != null && src != null) { - url = url.substring(url.lastIndexOf('/')+1, url.length()); - url = url.substring(url.indexOf("%3A")+3, url.length()); - int dotIndex = url.lastIndexOf('.'); - File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), url); - String newurl = url; - for (int i = 2; storageDir.exists(); i++) { - newurl = url.substring(0, dotIndex) + "_" + i + url.substring(dotIndex, url.length()); - storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), newurl); - } - - Uri source = Uri.parse(src); - Uri picUri = Uri.fromFile(storageDir); - - String toastText; - try { - InputStream istream = getActivity().getContentResolver().openInputStream(source); - OutputStream ostream = new FileOutputStream(storageDir); - - byte[] buffer = new byte[1024]; - int len; - while((len = istream.read(buffer)) > 0) { - ostream.write(buffer, 0, len); - } - - istream.close(); - ostream.close(); - } catch (IOException e) { - Log.d("kiwix", "Couldn't save image", e); - toastText = getResources().getString(R.string.save_image_error); - } finally { - toastText = String.format(getResources().getString(R.string.save_image_saved), newurl); - } - - Toast toast = Toast.makeText(getActivity().getApplicationContext(), toastText, Toast.LENGTH_LONG); - toast.show(); - } - } - }; - - final Handler viewHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - String url = (String) msg.getData().get("url"); + final Handler saveHandler = new + Handler() { - if (url != null) { - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); - startActivity(intent); - } - } - }; + @Override + public void handleMessage(Message msg) { + String url = (String) msg.getData().get("url"); + String src = (String) msg.getData().get("src"); + + if (url != null && src != null) { + url = url.substring(url.lastIndexOf('/') + 1, url.length()); + url = url.substring(url.indexOf("%3A") + 3, url.length()); + int dotIndex = url.lastIndexOf('.'); + File storageDir = new File(Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_PICTURES), url); + String newurl = url; + for (int i = 2; storageDir.exists(); i++) { + newurl = url.substring(0, dotIndex) + "_" + i + url + .substring(dotIndex, url.length()); + storageDir = new File(Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_PICTURES), newurl); + } + + Uri source = Uri.parse(src); + Uri picUri = Uri.fromFile(storageDir); + + String toastText; + try { + InputStream istream = getActivity().getContentResolver() + .openInputStream(source); + OutputStream ostream = new FileOutputStream(storageDir); + + byte[] buffer = new byte[1024]; + int len; + while ((len = istream.read(buffer)) > 0) { + ostream.write(buffer, 0, len); + } + + istream.close(); + ostream.close(); + } catch (IOException e) { + Log.d("kiwix", "Couldn't save image", e); + toastText = getResources().getString(R.string.save_image_error); + } finally { + toastText = String + .format(getResources().getString(R.string.save_image_saved), newurl); + } + + Toast toast = Toast.makeText(getActivity().getApplicationContext(), toastText, + Toast.LENGTH_LONG); + toast.show(); + } + } + }; + + final Handler viewHandler = new + + Handler() { + @Override + public void handleMessage(Message msg) { + String url = (String) msg.getData().get("url"); + + if (url != null) { + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + startActivity(intent); + } + } + }; // Image long-press webView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { - @Override - public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { final HitTestResult result = ((WebView) v).getHitTestResult(); if (result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) { - menu.add(0, 1, 0, getResources().getString(R.string.save_image)).setOnMenuItemClickListener(new OnMenuItemClickListener() { - public boolean onMenuItemClick(MenuItem item) { - Message msg = saveHandler.obtainMessage(); - webView.requestFocusNodeHref(msg); - return true; - } - }); - } - } + menu.add(0, 1, 0, getResources().getString(R.string.save_image)) + .setOnMenuItemClickListener( + new OnMenuItemClickListener() { + public boolean onMenuItemClick(MenuItem item) { + Message msg = saveHandler.obtainMessage(); + webView.requestFocusNodeHref(msg); + return true; + } + }); + } + } }); // js includes will not happen unless we enable JS