Java tutorial
/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.alerts.activity; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import com.liferay.alerts.R; import com.liferay.alerts.adapter.CommentListAdapter; import com.liferay.alerts.callback.AddCommentCallback; import com.liferay.alerts.callback.FetchCommentsCallback; import com.liferay.alerts.model.Alert; import com.liferay.alerts.model.AlertType; import com.liferay.alerts.model.User; import com.liferay.alerts.util.PortraitUtil; import com.liferay.alerts.util.SettingsUtil; import com.liferay.alerts.util.ToastUtil; import com.liferay.alerts.util.Validator; import com.liferay.alerts.widget.EditText; import com.liferay.alerts.widget.TextView; import com.liferay.mobile.android.service.Session; import com.liferay.mobile.android.task.callback.AsyncTaskCallback; import com.liferay.mobile.android.v62.pushnotificationsentry.PushnotificationsentryService; import java.util.ArrayList; import org.json.JSONObject; /** * @author Bruno Farache */ public class CommentsActivity extends Activity implements OnClickListener { public static final String ACTION_ADD_COMMENT = "ACTION_ADD_COMMENT"; public static final String ACTION_UPDATE_COMMENTS_LIST = "ACTION_UPDATE_COMMENTS_LIST"; public static final String EXTRA_ALERT = "EXTRA_ALERT"; public static final String EXTRA_ALERTS = "EXTRA_ALERTS"; public static void addComment(Context context, Alert alert) { Intent intent = new Intent(ACTION_ADD_COMMENT); intent.putExtra(EXTRA_ALERT, alert); LocalBroadcastManager.getInstance(context).sendBroadcast(intent); } public static void updateCommentsList(Context context, ArrayList<Alert> alerts) { Intent intent = new Intent(ACTION_UPDATE_COMMENTS_LIST); intent.putExtra(EXTRA_ALERTS, alerts); LocalBroadcastManager.getInstance(context).sendBroadcast(intent); } @Override public void onClick(View view) { String message = _edit.getText().toString(); if (Validator.isNull(message)) { return; } AsyncTaskCallback callback = new AddCommentCallback(this); Session session = SettingsUtil.getSession(callback); PushnotificationsentryService service = new PushnotificationsentryService(session); try { JSONObject payload = new JSONObject(); payload.put(Alert.MESSAGE, message); payload.put(Alert.TYPE, AlertType.TEXT.toString()); service.addPushNotificationsEntry(_alert.getId(), payload.toString()); } catch (Exception e) { _handleException(e); } } @Override public void onCreate(Bundle state) { super.onCreate(state); setContentView(R.layout.comments); _alert = getIntent().getExtras().getParcelable(EXTRA_ALERT); TextView message = (TextView) findViewById(R.id.message); message.setText(_alert.getMessage()); setBackListener(); User user = _alert.getUser(this); TextView userName = (TextView) findViewById(R.id.user_name); ImageView portrait = (ImageView) findViewById(R.id.portrait); PortraitUtil.setUserDetails(this, user, portrait, userName); _edit = (EditText) findViewById(R.id.edit); ImageView post = (ImageView) findViewById(R.id.post); post.setOnClickListener(this); _registerBroadcastReceiver(); AsyncTaskCallback callback = new FetchCommentsCallback(getApplicationContext()); Session session = SettingsUtil.getSession(callback); PushnotificationsentryService service = new PushnotificationsentryService(session); try { service.getPushNotificationsEntries(_alert.getId(), 0, -1, -1); } catch (Exception e) { ToastUtil.show(this, e.getMessage()); } } @Override protected void onDestroy() { _getBroadcastManager().unregisterReceiver(_receiver); super.onDestroy(); } protected void setBackListener() { View navigator = findViewById(R.id.navigator); navigator.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { finish(); } }); } private LocalBroadcastManager _getBroadcastManager() { return LocalBroadcastManager.getInstance(this); } private void _handleException(Exception exception) { String message = getString(R.string.post_failure); message = message + exception.getMessage(); ToastUtil.show(this, message, true); } private void _registerBroadcastReceiver() { IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_ADD_COMMENT); filter.addAction(ACTION_UPDATE_COMMENTS_LIST); _receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); ListView listView = (ListView) findViewById(R.id.comments); if (ACTION_UPDATE_COMMENTS_LIST.equals(action)) { ArrayList<Alert> alerts = intent.getParcelableArrayListExtra(EXTRA_ALERTS); ArrayAdapter<Alert> adapter = new CommentListAdapter(getApplicationContext(), alerts); listView.setAdapter(adapter); } else if (ACTION_ADD_COMMENT.equals(action)) { Alert alert = intent.getParcelableExtra(EXTRA_ALERT); ArrayAdapter<Alert> adapter = (ArrayAdapter<Alert>) listView.getAdapter(); adapter.add(alert); } } }; _getBroadcastManager().registerReceiver(_receiver, filter); } private Alert _alert; private EditText _edit; private BroadcastReceiver _receiver; }