Advertisement
Guest User

Untitled

a guest
Oct 21st, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.29 KB | None | 0 0
  1. public class ChatFragment extends Fragment implements Constants {
  2.  
  3. public final static int STATUS_START = 100;
  4.  
  5. public final static String PARAM_TASK = "task";
  6. public final static String PARAM_STATUS = "status";
  7.  
  8.  
  9. final String LOG_TAG = "myLogs";
  10.  
  11. public static final int RESULT_OK = -1;
  12.  
  13. private ProgressDialog pDialog;
  14.  
  15. Menu MainMenu;
  16.  
  17. View mListViewHeader;
  18.  
  19. RelativeLayout mLoadingScreen, mErrorScreen;
  20. LinearLayout mContentScreen, mContainerImg, mChatListViewHeaderContainer;
  21.  
  22. ImageView mSendMessage, mAddImg, mDeleteImg, mPreviewImg;
  23. EditText mMessageText;
  24.  
  25. ListView listView;
  26.  
  27. BroadcastReceiver br;
  28.  
  29. private ArrayList<ChatItem> chatList;
  30.  
  31. private ChatListAdapter chatAdapter;
  32.  
  33. String withProfile = "", messageText = "", messageImg = "";
  34. int chatId = 0, msgId = 0, messagesCount = 0, position = 0;
  35. long profileId = 0;
  36.  
  37. private String selectedChatImg = "";
  38. private Uri selectedImage;
  39.  
  40. int arrayLength = 0;
  41. Boolean loadingMore = false;
  42. Boolean viewMore = false;
  43.  
  44. private Boolean loading = false;
  45. private Boolean restore = false;
  46. private Boolean preload = false;
  47.  
  48. public ChatFragment() {
  49. // Required empty public constructor
  50. }
  51.  
  52. @Override
  53. public void onCreate(Bundle savedInstanceState) {
  54.  
  55. super.onCreate(savedInstanceState);
  56.  
  57. setRetainInstance(true);
  58.  
  59. setHasOptionsMenu(true);
  60.  
  61. initpDialog();
  62.  
  63. Intent i = getActivity().getIntent();
  64. position = i.getIntExtra("position", 0);
  65. chatId = i.getIntExtra("chatId", 0);
  66. profileId = i.getLongExtra("profileId", 0);
  67. withProfile = i.getStringExtra("withProfile");
  68. chatList = new ArrayList<ChatItem>();
  69. chatAdapter = new ChatListAdapter(getActivity(), chatList);
  70. }
  71.  
  72. @Override
  73. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  74.  
  75. View rootView = inflater.inflate(R.layout.fragment_chat, container, false);
  76.  
  77. if (savedInstanceState != null) {
  78.  
  79. restore = savedInstanceState.getBoolean("restore");
  80. loading = savedInstanceState.getBoolean("loading");
  81. preload = savedInstanceState.getBoolean("preload");
  82.  
  83. } else {
  84.  
  85. App.getInstance().setCurrentChatId(chatId);
  86.  
  87. restore = false;
  88. loading = false;
  89. preload = false;
  90. }
  91.  
  92. br = new BroadcastReceiver() {
  93.  
  94. public void onReceive(Context context, Intent intent) {
  95.  
  96. int task = intent.getIntExtra(PARAM_TASK, 0);
  97. int status = intent.getIntExtra(PARAM_STATUS, 0);
  98. Log.d(LOG_TAG, "onReceive: task = " + task + ", status = " + status);
  99.  
  100. getNextMessages();
  101. }
  102. };
  103.  
  104. IntentFilter intFilt = new IntentFilter(BROADCAST_ACTION);
  105. getActivity().registerReceiver(br, intFilt);
  106.  
  107. if (loading) {
  108.  
  109. showpDialog();
  110. }
  111.  
  112. mLoadingScreen = (RelativeLayout) rootView.findViewById(R.id.loadingScreen);
  113. mErrorScreen = (RelativeLayout) rootView.findViewById(R.id.errorScreen);
  114.  
  115. mContentScreen = (LinearLayout) rootView.findViewById(R.id.contentScreen);
  116.  
  117. mSendMessage = (ImageView) rootView.findViewById(R.id.sendMessage);
  118. mMessageText = (EditText) rootView.findViewById(R.id.messageText);
  119.  
  120. mSendMessage.setOnClickListener(new View.OnClickListener() {
  121. @Override
  122. public void onClick(View v) {
  123.  
  124. newMessage();
  125. }
  126. });
  127.  
  128. listView = (ListView) rootView.findViewById(R.id.listView);
  129.  
  130. mListViewHeader = getActivity().getLayoutInflater().inflate(R.layout.chat_listview_header, null);
  131. mChatListViewHeaderContainer = (LinearLayout) mListViewHeader.findViewById(R.id.chatListViewHeaderContainer);
  132.  
  133. listView.addHeaderView(mListViewHeader);
  134.  
  135. mListViewHeader.setVisibility(View.GONE);
  136.  
  137. listView.setAdapter(chatAdapter);
  138.  
  139. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  140.  
  141. @Override
  142. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  143.  
  144. if (position == 0 && mListViewHeader.getVisibility() == View.VISIBLE) {
  145.  
  146. getPreviousMessages();
  147. }
  148. }
  149. });
  150.  
  151. mAddImg = (ImageView) rootView.findViewById(R.id.addImg);
  152. mDeleteImg = (ImageView) rootView.findViewById(R.id.deleteImg);
  153. mPreviewImg = (ImageView) rootView.findViewById(R.id.previewImg);
  154.  
  155. mContainerImg = (LinearLayout) rootView.findViewById(R.id.container_img);
  156.  
  157. mContainerImg.setVisibility(View.GONE);
  158.  
  159. mDeleteImg.setOnClickListener(new View.OnClickListener(){
  160. @Override
  161. public void onClick(View v) {
  162.  
  163. selectedImage = null;
  164. selectedChatImg = "";
  165.  
  166. mContainerImg.setVisibility(View.GONE);
  167. }
  168. });
  169.  
  170. mAddImg.setOnClickListener(new View.OnClickListener() {
  171.  
  172. @Override
  173. public void onClick(View v) {
  174.  
  175. Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  176. startActivityForResult(Intent.createChooser(intent, getText(R.string.label_select_img)), SELECT_POST_IMG);
  177. }
  178. });
  179.  
  180. if (selectedImage != null) {
  181.  
  182. mPreviewImg.setImageURI(selectedImage);
  183. mContainerImg.setVisibility(View.VISIBLE);
  184. }
  185.  
  186. if (!restore) {
  187.  
  188. if (App.getInstance().isConnected()) {
  189.  
  190. showLoadingScreen();
  191. getChat();
  192.  
  193. } else {
  194.  
  195. showErrorScreen();
  196. }
  197.  
  198. } else {
  199.  
  200. if (App.getInstance().isConnected()) {
  201.  
  202. if (!preload) {
  203.  
  204. showContentScreen();
  205.  
  206. } else {
  207.  
  208. showLoadingScreen();
  209. }
  210.  
  211. } else {
  212.  
  213. showErrorScreen();
  214. }
  215. }
  216.  
  217. // Inflate the layout for this fragment
  218. return rootView;
  219. }
  220.  
  221. public void onDestroyView() {
  222.  
  223. super.onDestroyView();
  224.  
  225. getActivity().unregisterReceiver(br);
  226.  
  227. hidepDialog();
  228. }
  229.  
  230. protected void initpDialog() {
  231.  
  232. pDialog = new ProgressDialog(getActivity());
  233. pDialog.setMessage(getString(R.string.msg_loading));
  234. pDialog.setCancelable(false);
  235. }
  236.  
  237. protected void showpDialog() {
  238.  
  239. if (!pDialog.isShowing()) pDialog.show();
  240. }
  241.  
  242. protected void hidepDialog() {
  243.  
  244. if (pDialog.isShowing()) pDialog.dismiss();
  245. }
  246.  
  247. @Override
  248. public void onSaveInstanceState(Bundle outState) {
  249.  
  250. super.onSaveInstanceState(outState);
  251.  
  252. outState.putBoolean("restore", true);
  253. outState.putBoolean("loading", loading);
  254. outState.putBoolean("preload", preload);
  255. }
  256.  
  257. @Override
  258. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  259.  
  260. super.onActivityResult(requestCode, resultCode, data);
  261.  
  262. if (requestCode == SELECT_POST_IMG && resultCode == RESULT_OK && null != data) {
  263.  
  264. selectedImage = data.getData();
  265. String[] filePathColumn = {MediaStore.Images.Media.DATA};
  266.  
  267. Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
  268. cursor.moveToFirst();
  269.  
  270. // String selectedPhoto contains the path of selected Image
  271. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  272. selectedChatImg = cursor.getString(columnIndex);
  273. cursor.close();
  274.  
  275. mPreviewImg.setImageURI(selectedImage);
  276.  
  277. mContainerImg.setVisibility(View.VISIBLE);
  278. }
  279. }
  280.  
  281. private void scrollListViewToBottom() {
  282.  
  283. listView.post(new Runnable() {
  284. @Override
  285. public void run() {
  286. // Select the last row so it will scroll into view...
  287. listView.setSelection(chatAdapter.getCount() - 1);
  288. }
  289. });
  290. }
  291.  
  292. public void getChat() {
  293.  
  294. preload = true;
  295.  
  296. CustomRequest jsonReq = new CustomRequest(Request.Method.POST, METHOD_CHAT_GET, null,
  297. new Response.Listener<JSONObject>() {
  298. @Override
  299. public void onResponse(JSONObject response) {
  300.  
  301. try {
  302.  
  303. // Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_LONG).show();
  304.  
  305. if (!response.getBoolean("error")) {
  306.  
  307. msgId = response.getInt("msgId");
  308. chatId = response.getInt("chatId");
  309. messagesCount = response.getInt("messagesCount");
  310.  
  311. if (messagesCount > 20) {
  312.  
  313. mListViewHeader.setVisibility(View.VISIBLE);
  314. }
  315.  
  316. if (response.has("messages")) {
  317.  
  318. JSONArray messagesArray = response.getJSONArray("messages");
  319.  
  320. arrayLength = messagesArray.length();
  321.  
  322. if (arrayLength > 0) {
  323.  
  324. for (int i = messagesArray.length() - 1; i > -1; i--) {
  325.  
  326. JSONObject msgObj = (JSONObject) messagesArray.get(i);
  327.  
  328. ChatItem item = new ChatItem(msgObj);
  329.  
  330. chatList.add(item);
  331. }
  332. }
  333. }
  334. }
  335.  
  336. } catch (JSONException e) {
  337.  
  338. e.printStackTrace();
  339.  
  340. } finally {
  341.  
  342. showContentScreen();
  343.  
  344. chatAdapter.notifyDataSetChanged();
  345.  
  346. scrollListViewToBottom();
  347. }
  348. }
  349. }, new Response.ErrorListener() {
  350. @Override
  351. public void onErrorResponse(VolleyError error) {
  352.  
  353. preload = false;
  354.  
  355. Toast.makeText(getActivity(), getString(R.string.error_data_loading), Toast.LENGTH_SHORT).show();
  356. }
  357. }) {
  358.  
  359. @Override
  360. protected Map<String, String> getParams() {
  361. Map<String, String> params = new HashMap<String, String>();
  362. params.put("accountId", Long.toString(App.getInstance().getId()));
  363. params.put("accessToken", App.getInstance().getAccessToken());
  364.  
  365. params.put("profileId", Long.toString(profileId));
  366.  
  367. params.put("chatId", Integer.toString(chatId));
  368. params.put("msgId", Integer.toString(msgId));
  369.  
  370. return params;
  371. }
  372. };
  373.  
  374. App.getInstance().addToRequestQueue(jsonReq);
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement