Android Open Source - SciProApp-Android New Message






From Project

Back to project page SciProApp-Android.

License

The source code is released under:

Apache License

If you think the Android project SciProApp-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2011 Patrick Strang./*from w ww.j a  va2s  . c o  m*/
 *
 * 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 se.su.dsv.scipro.android.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import se.su.dsv.scipro.android.IHeaderOnClick;
import se.su.dsv.scipro.android.R;
import se.su.dsv.scipro.android.SciProApplication;
import se.su.dsv.scipro.android.dao.PrivateMessage;
import se.su.dsv.scipro.android.dao.User;
import se.su.dsv.scipro.android.helpers.MenuHelper;
import se.su.dsv.scipro.android.tasks.SendMessageAsyncTask;
import se.su.dsv.scipro.android.utils.SciProUtils;

public class NewMessage extends Activity implements IHeaderOnClick, SendMessageAsyncTask.SentMessageResponder {

    public static final String TAG = "NewMessage";

    private AutoCompleteTextView recipientField;
    private TextView subjectField;
    private EditText messageTextField;
    private Button submitButton;
    private PrivateMessage replyToMessage;

    private ProgressDialog sendingMessageInProgress;

    protected boolean changesPending;
    private AlertDialog unsavedChangesDialog;
    private ArrayAdapter<User> adapter;
    private User selectedRecipient = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if (bundle.getSerializable("message") != null) {
                replyToMessage = (PrivateMessage) bundle.getSerializable("message");
                selectedRecipient = replyToMessage.from;
            }
            
        }

        setContentView(R.layout.activity_compose_message);

        setUpViews();
    }

    @Override
    public void onBackPressed() {
        cancel();
    }

    protected void cancel() {
        if (changesPending) {
            unsavedChangesDialog = new AlertDialog.Builder(this)
                    .setTitle("Changes made")
                    .setMessage("You have made changes.")
                    .setPositiveButton(R.string.send_message, new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            sendMessage();
                        }
                    })
                    .setNeutralButton(R.string.discard, new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            unsavedChangesDialog.cancel();
                        }
                    })
                    .create();
            unsavedChangesDialog.show();
        } else {
            finish();
        }
    }

    protected void sendMessage() {

    }

    protected boolean validInputFields() {
        boolean valid = false;

        valid = recipientField.getText().length() > 0 && selectedRecipient != null;

        return valid;
    }

    private void setUpViews() {
        recipientField = (AutoCompleteTextView) findViewById(R.id.to_edit);
        subjectField = (TextView) findViewById(R.id.subject_edit);
        messageTextField = (EditText) findViewById(R.id.message_edit);
        submitButton = (Button) findViewById(R.id.send_message);

        adapter = new ArrayAdapter<User>(this,
                android.R.layout.simple_dropdown_item_1line, SciProApplication.getInstance().getUsers());
        recipientField.setAdapter(adapter);

        recipientField.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                changesPending = true;
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        });

        recipientField.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                User u = (User) (parent.getItemAtPosition(position));
                Log.d(TAG, "Added " + u.name + " to list");
                selectedRecipient = u;
            }
        });

        subjectField.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                changesPending = true;
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        });

        if (replyToMessage != null) {
            subjectField.setText("Re: " + replyToMessage.subject);
            recipientField.setText(selectedRecipient.toString());
        }

        submitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (validInputFields()) {
//                    Log.d(TAG, subjectField.getText().toString());
//                    Log.d(TAG, messageTextField.getText().toString());
//                    Log.d(TAG, "" + selectedRecipients.size());
                    new SendMessageAsyncTask(NewMessage.this,
                            subjectField.getText().toString(),
                            messageTextField.getText().toString(),
                            selectedRecipient)
                            .execute();
                } else {
                    Toast.makeText(NewMessage.this, "The message must have a valid recipient.", Toast.LENGTH_LONG).show();
                }
            }
        });

        ImageButton homeButton = (ImageButton) findViewById(R.id.header_home_btn);
        homeButton.setVisibility(View.VISIBLE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return MenuHelper.openActivityFromMenuItem(this, item);
    }

    public void onHeaderHomeClick(View v) {
        SciProUtils.openHomeActivity(this);
    }

    public void onHeaderMessagesClick(View v) {
        SciProUtils.openMessagesActivity(this);
    }

    public void messageSent(SendMessageAsyncTask.SentMessageResult result) {
        sendingMessageInProgress.dismiss();

        finish();
    }

    public void sendingMessage() {
        sendingMessageInProgress = ProgressDialog.show(this,
                "Sending",
                "Sending message.");
    }
}




Java Source Code List

se.su.dsv.scipro.android.IHeaderOnClick.java
se.su.dsv.scipro.android.Preferences.java
se.su.dsv.scipro.android.SciProApplication.java
se.su.dsv.scipro.android.activity.Authenticate.java
se.su.dsv.scipro.android.activity.MainActivity.java
se.su.dsv.scipro.android.activity.NewMessage.java
se.su.dsv.scipro.android.activity.PrivateMessageView.java
se.su.dsv.scipro.android.activity.PrivateMessages.java
se.su.dsv.scipro.android.activity.ProjectView.java
se.su.dsv.scipro.android.activity.StatusCheckIn.java
se.su.dsv.scipro.android.activity.SupervisorHome.java
se.su.dsv.scipro.android.adapters.MessageListAdapter.java
se.su.dsv.scipro.android.adapters.ProjectListAdapter.java
se.su.dsv.scipro.android.dao.FinalSeminar.java
se.su.dsv.scipro.android.dao.PrivateMessage.java
se.su.dsv.scipro.android.dao.Project.java
se.su.dsv.scipro.android.dao.User.java
se.su.dsv.scipro.android.helpers.MenuHelper.java
se.su.dsv.scipro.android.json.SciProJSON.java
se.su.dsv.scipro.android.location.LocationIntentReceiver.java
se.su.dsv.scipro.android.providers.SciProProvider.java
se.su.dsv.scipro.android.services.UpdaterService.java
se.su.dsv.scipro.android.tasks.AuthAsyncTask.java
se.su.dsv.scipro.android.tasks.GetMessagesAsyncTask.java
se.su.dsv.scipro.android.tasks.GetProjectsAsyncTask.java
se.su.dsv.scipro.android.tasks.LoginAsyncTask.java
se.su.dsv.scipro.android.tasks.LoginResponder.java
se.su.dsv.scipro.android.tasks.LoginResult.java
se.su.dsv.scipro.android.tasks.SendMessageAsyncTask.java
se.su.dsv.scipro.android.tasks.SetMessageReadAsyncTask.java
se.su.dsv.scipro.android.tasks.SetStatusAsyncTask.java
se.su.dsv.scipro.android.utils.Base64Coder.java
se.su.dsv.scipro.android.utils.DaoUtils.java
se.su.dsv.scipro.android.utils.SciProUtils.java
se.su.dsv.scipro.android.utils.StringUtils.java
se.su.dsv.scipro.android.view.MessageListItem.java
se.su.dsv.scipro.android.view.ProjectListItem.java