Java tutorial
/* * Copyright (C) 2012 CONIT Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.co.conit.sss.sn.ex2.fragment; import jp.co.conit.sss.sn.ex2.R; import jp.co.conit.sss.sn.ex2.activity.UserDataActivity; import jp.co.conit.sss.sn.ex2.entitiy.PushMessage; import jp.co.conit.sss.sn.ex2.entitiy.SNServerResult; import jp.co.conit.sss.sn.ex2.util.PrefrerencesUtil; import jp.co.conit.sss.sn.ex2.util.SNApiUtil; import jp.co.conit.sss.sn.ex2.util.StringUtil; import org.apache.http.HttpStatus; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.ListFragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.TextView; /** * ????Fragment?? * * @author conit */ public class MessagesFragment extends ListFragment { private static final int INTERNAL_EMPTY_ID = 0x00ff0001; private static final String TAG = "MessagesFragment"; private static MessagesFragment mSelf; private static Activity mParentActivity; private SNMessagesServerTask mMessagesTask; private PushMessagesAdapter mPushMessagesAdapter; @Override public void onAttach(Activity activity) { super.onAttach(activity); mParentActivity = activity; mSelf = this; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setEmptyText(getString(R.string.no_messages)); // EmptyText????INTERNAL_EMPTY_ID??TextView????? // INTERNAL_EMPTY_ID?ListFragment??EmptyText?ID?????????????ListFragment??????? TextView tx = (TextView) getView().findViewById(INTERNAL_EMPTY_ID); int color = getResources().getColor(R.color.settings_text_color); tx.setTextColor(color); getListView().setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { PushMessage pm = (PushMessage) parent.getItemAtPosition(position); String userData = pm.getUserData(); Intent activityIntent = new Intent(); if (StringUtil.isEmpty(userData) || userData.equals("null")) { return; } else { if (userData.startsWith("http")) { activityIntent.setAction(Intent.ACTION_VIEW); activityIntent.setData(Uri.parse(userData)); } else { activityIntent.setClass(mParentActivity, UserDataActivity.class); activityIntent.putExtra("option", userData); } } startActivity(activityIntent); } }); obtainMessagesAsync(); } @Override public void onDestroy() { super.onDestroy(); if (mMessagesTask != null && !mMessagesTask.isCancelled()) { mMessagesTask.cancel(true); } } /** * ??????? */ private void obtainMessagesAsync() { mMessagesTask = new SNMessagesServerTask(); mMessagesTask.execute(getViewType()); } /** * SharedPreferences????? */ private String getViewType() { return PrefrerencesUtil.getString(getActivity(), "view_type", ""); } /** * SamuraiNotification?????? * * @author conit */ public class SNMessagesServerTask extends AsyncTask<String, Void, SNServerResult> { public SNMessagesServerTask() { } @Override protected SNServerResult doInBackground(String... args) { return SNApiUtil.messages(args[0]); } @Override protected void onPostExecute(SNServerResult result) { if (result.mHttpStatus == HttpStatus.SC_OK) { mPushMessagesAdapter = new PushMessagesAdapter(getActivity(), R.layout.list_item_push_message); setListAdapter(mPushMessagesAdapter); parseMessaes(result.mResponseString); } else { Log.e(TAG, "status code:" + result.mHttpStatus); showAlertDialogFragment(AlertDialogFragment.PUSH_MESSAGES); } } /** * JSONPushMessage???? * * @param response * @return */ private void parseMessaes(String response) { try { JSONObject jsonObject = new JSONObject(response); JSONArray jsonArray = jsonObject.getJSONArray("messages"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject object = jsonArray.getJSONObject(i); String mid = object.getString("mid"); String ticker = object.getString("sn_ticker"); String title = object.getString("sn_title"); String text = object.getString("sn_text"); String userData = object.getString("sn_userdata"); String dateStr = object.getString("message_date"); PushMessage pm = new PushMessage(); pm.setMid(mid); pm.setTicker(ticker); pm.setTitle(title); pm.setText(text); pm.setDateStr(dateStr); pm.setUserData(userData); mPushMessagesAdapter.add(pm); } } catch (JSONException e) { e.printStackTrace(); } mPushMessagesAdapter.notifyDataSetChanged(); } } /** * Samurai Notification???????? * * @author */ class PushMessagesAdapter extends ArrayAdapter<PushMessage> { public PushMessagesAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } private LayoutInflater mInflater; @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ViewHolder viewHolder; if (v == null) { v = mInflater.inflate(R.layout.list_item_push_message, parent, false); TextView date = (TextView) v.findViewById(R.id.text_date); viewHolder = new ViewHolder(); TextView text = (TextView) v.findViewById(R.id.text_text); viewHolder.textText = text; viewHolder.dateText = date; v.setTag(viewHolder); } else { viewHolder = (ViewHolder) v.getTag(); } PushMessage item = getItem(position); viewHolder.textText.setText(item.getText()); viewHolder.dateText.setText(item.getDateStr()); return v; } } private static class ViewHolder { TextView textText; TextView dateText; } /** * ?????? * * @author conit */ private static class AlertDialogFragment extends DialogFragment { public static final int PUSH_MESSAGES = 1; public static AlertDialogFragment newInstance(int type) { AlertDialogFragment frag = new AlertDialogFragment(); Bundle args = new Bundle(); args.putInt("dialog_type", type); frag.setArguments(args); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Bundle arg = getArguments(); int type = arg.getInt("dialog_type"); AlertDialog.Builder dlg = new AlertDialog.Builder(getActivity()); switch (type) { case PUSH_MESSAGES: dlg.setTitle(getString(R.string.dialog_err_network)); dlg.setMessage(getString(R.string.dialog_err_push_messages)); dlg.setCancelable(false); dlg.setPositiveButton(getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int whichButton) { mSelf.removeDialogFragment(PUSH_MESSAGES); mParentActivity.finish(); } }); break; default: break; } return dlg.create(); } } /** * ??? * * @param type */ private void showAlertDialogFragment(int type) { FragmentManager fm = getFragmentManager(); AlertDialogFragment adf = AlertDialogFragment.newInstance(type); adf.show(fm, Integer.toString(type)); } /** * ????????? * * @param type */ private void removeDialogFragment(int type) { FragmentTransaction ft = getFragmentManager().beginTransaction(); DialogFragment prev = (DialogFragment) getFragmentManager().findFragmentByTag(Integer.toString(type)); if (prev != null) { prev.dismiss(); ft.remove(prev); ft.commit(); } } }