|
1 | | -package org.wordpress.editor; |
| 1 | +package org.wordpress.android.editor; |
2 | 2 |
|
3 | 3 | import android.annotation.SuppressLint; |
4 | 4 | import android.content.res.AssetManager; |
5 | 5 | import android.os.Bundle; |
6 | | -import android.support.v7.app.ActionBarActivity; |
| 6 | +import android.support.v4.app.Fragment; |
7 | 7 | import android.util.Log; |
| 8 | +import android.view.LayoutInflater; |
| 9 | +import android.view.View; |
| 10 | +import android.view.ViewGroup; |
8 | 11 | import android.webkit.ConsoleMessage; |
9 | 12 | import android.webkit.JsResult; |
10 | 13 | import android.webkit.WebChromeClient; |
|
17 | 20 | import java.io.InputStream; |
18 | 21 | import java.io.InputStreamReader; |
19 | 22 |
|
20 | | -public class EditorActivity extends ActionBarActivity { |
21 | | - WebView mWebView; |
| 23 | +public class EditorFragment extends Fragment { |
| 24 | + private static final String ARG_PARAM_TITLE = "param_title"; |
| 25 | + private static final String ARG_PARAM_CONTENT = "param_content"; |
| 26 | + |
| 27 | + private String mParamTitle; |
| 28 | + private String mParamContent; |
| 29 | + private WebView mWebView; |
| 30 | + |
| 31 | + public static EditorFragment newInstance(String title, String content) { |
| 32 | + EditorFragment fragment = new EditorFragment(); |
| 33 | + Bundle args = new Bundle(); |
| 34 | + args.putString(ARG_PARAM_TITLE, title); |
| 35 | + args.putString(ARG_PARAM_CONTENT, content); |
| 36 | + fragment.setArguments(args); |
| 37 | + return fragment; |
| 38 | + } |
| 39 | + |
| 40 | + public EditorFragment() { |
| 41 | + } |
22 | 42 |
|
23 | | - @SuppressLint("SetJavaScriptEnabled") |
24 | 43 | @Override |
25 | | - protected void onCreate(Bundle savedInstanceState) { |
| 44 | + public void onCreate(Bundle savedInstanceState) { |
26 | 45 | super.onCreate(savedInstanceState); |
27 | | - setContentView(R.layout.activity_editor); |
28 | | - mWebView = (WebView) findViewById(R.id.webview); |
| 46 | + if (getArguments() != null) { |
| 47 | + mParamTitle = getArguments().getString(ARG_PARAM_TITLE); |
| 48 | + mParamContent = getArguments().getString(ARG_PARAM_CONTENT); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 54 | + Bundle savedInstanceState) { |
| 55 | + View view = inflater.inflate(R.layout.fragment_editor, container, false); |
| 56 | + mWebView = (WebView) view.findViewById(R.id.webview); |
| 57 | + initWebView(); |
| 58 | + return view; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onDetach() { |
| 63 | + super.onDetach(); |
| 64 | + } |
| 65 | + |
| 66 | + // TODO: use AppLog instead of Log |
| 67 | + @SuppressLint("SetJavaScriptEnabled") |
| 68 | + private void initWebView() { |
29 | 69 | WebSettings webSettings = mWebView.getSettings(); |
30 | 70 | webSettings.setJavaScriptEnabled(true); |
31 | 71 | webSettings.setDefaultTextEncodingName("utf-8"); |
32 | | - mWebView.getSettings().setJavaScriptEnabled(true); |
33 | 72 | mWebView.setWebViewClient(new WebViewClient() { |
34 | 73 | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { |
35 | 74 | Log.e("WordPress-Editor", description); |
@@ -57,7 +96,10 @@ public void onConsoleMessage(String message, int lineNumber, String sourceId) { |
57 | 96 | } |
58 | 97 |
|
59 | 98 | private String getStringFromAsset(String filename) throws IOException { |
60 | | - AssetManager assetManager = getAssets(); |
| 99 | + if (!isAdded()) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + AssetManager assetManager = getActivity().getAssets(); |
61 | 103 | InputStream in = assetManager.open(filename); |
62 | 104 | InputStreamReader is = new InputStreamReader(in); |
63 | 105 | StringBuilder sb = new StringBuilder(); |
|
0 commit comments