Back to project page android-textlater.
The source code is released under:
Apache License
If you think the Android project android-textlater listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2013 The Android Open Source Project *// w ww . ja va 2 s.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 com.michael.feng.textlater; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockListActivity; import com.actionbarsherlock.view.ActionMode; import com.actionbarsherlock.view.Menu; import com.google.ads.AdRequest; import com.google.ads.AdSize; import com.google.ads.AdView; import java.util.Calendar; import java.util.List; public class MainActivity extends SherlockListActivity implements OnClickListener { public static int THEME = R.style.Theme_Sherlock; private ListView textList; private MessageDAO messageDAO; private List<Message> messageList; ListViewAdapter listViewAdapter; ActionMode mMode; private ImageButton editButton; private ImageButton newButton; private AdView adView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Remove title bar // requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getSupportActionBar().setDisplayShowCustomEnabled(true); getSupportActionBar().setCustomView(R.layout.abs_layout); // Init Edit Button editButton = (ImageButton) findViewById(R.id.editButton); editButton.setOnClickListener(this); editButton.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // touch down code editButton.setBackgroundColor(Color.rgb(104, 156, 210)); break; case MotionEvent.ACTION_MOVE: // touch move code break; case MotionEvent.ACTION_UP: // touch up code editButton.setBackgroundColor(Color.TRANSPARENT); break; } return false; } }); // Init New Button newButton = (ImageButton) findViewById(R.id.newButton); newButton.setOnClickListener(this); newButton.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // touch down code newButton.setBackgroundColor(Color.rgb(104, 156, 210)); break; case MotionEvent.ACTION_MOVE: // touch move code break; case MotionEvent.ACTION_UP: // touch up code newButton.setBackgroundColor(Color.TRANSPARENT); break; } return false; } }); // Init ListView textList = (ListView) findViewById(android.R.id.list); listMessages(); // Create the adView adView = new AdView(this, AdSize.BANNER, "a151a6120560afe"); // Lookup your LinearLayout assuming it's been given // the attribute android:id="@+id/mainLayout" LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout); // Add the adView to it layout.addView(adView); // Initiate a generic request to load it with an ad adView.loadAd(new AdRequest()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // MenuInflater inflater = getSupportMenuInflater(); // inflater.inflate(R.menu.mainactivity_itemlist, menu); return true; } protected void showDeleteLayout() { listViewAdapter = new ListViewAdapter(this); listViewAdapter.setListLayout(R.layout.listitem_main_delete); textList.setAdapter(listViewAdapter); } protected void showDefaultLayout() { listViewAdapter = new ListViewAdapter(this); textList.setAdapter(listViewAdapter); } @Override protected void onResume() { listMessages(); super.onResume(); } // Get messages from DB Distinct TextContact, put into ListView public void listMessages() { messageDAO = new MessageDAO(this); messageDAO.open(); messageList = (List<Message>) messageDAO.getMessagesDistinctNumber(); messageDAO.close(); listViewAdapter = new ListViewAdapter(this); textList.setAdapter(listViewAdapter); } public void deleteMessages(String textContact) { messageDAO = new MessageDAO(this); messageDAO.open(); messageDAO.deleteMessageByContact(textContact); messageDAO.close(); } public class ListViewAdapter extends BaseAdapter { private LayoutInflater mInflater; private int listLayout = R.layout.listitem_main; public ListViewAdapter(Context con) { mInflater = LayoutInflater.from(con); } public void setListLayout(int layoutId) { listLayout = layoutId; } public int getCount() { return messageList.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { final ListContent holder; View view = convertView; if (view == null) { view = mInflater.inflate(listLayout, null); holder = new ListContent(); holder.textContact = (TextView) view .findViewById(R.id.ItemContact); holder.textContent = (TextView) view .findViewById(R.id.ItemText); holder.textWhen = (TextView) view.findViewById(R.id.ItemWhen); holder.deleteImg = (ImageView) view .findViewById(R.id.deleteImg); holder.groupImg = (ImageView) view.findViewById(R.id.groupImg); view.setTag(holder); } else { holder = (ListContent) view.getTag(); } // Display Group Image if contact number > 2 String displayContact = messageList.get(position).getTextContact(); if (displayContact.indexOf(";") > 0) { holder.groupImg.setVisibility(View.VISIBLE); } else { holder.groupImg.setVisibility(View.GONE); } // Display Contact name such as "Michael Feng ...", fix length if (displayContact.length() > 13) { displayContact = displayContact.substring(0, 12) + " ..."; } holder.textContact.setText(displayContact); holder.textContent.setText(messageList.get(position) .getTextContent()); // Display Date and Status such as "05.25 14:21 Done" int charIndex = messageList.get(position).getTextWhen() .indexOf(".") + 1; String displayDate = messageList.get(position).getTextWhen() .substring(charIndex); if ("1".equals(messageList.get(position).getHasSent())) { holder.textWhen.setText(displayDate + " Done"); } else { holder.textWhen.setText(displayDate + " Pending"); } holder.textContact.setOnClickListener(contentListener); holder.textContent.setOnClickListener(contentListener); holder.deleteImg.setOnClickListener(deleteImgListener); return view; } } static class ListContent { ImageView groupImg; TextView textContact; TextView textContent; TextView textWhen; ImageView deleteImg; } public OnClickListener contentListener = new OnClickListener() { public void onClick(View view) { int position = textList.getPositionForView((View) view.getParent()); // Put contact number into intent start Detail Activity Intent intent = new Intent( "com.michael.feng.textlater.DetailActivity"); String textContact = messageList.get(position).getTextContact(); intent.putExtra("textContact", textContact); startActivity(intent); } }; public OnClickListener deleteImgListener = new OnClickListener() { public void onClick(View view) { int position = textList.getPositionForView((View) view.getParent()); // Cancel alart task String textNumber = messageList.get(position).getTextNumber(); cancelTaskByContactNumber(textNumber); // Delete message by contact String textContact = messageList.get(position).getTextContact(); deleteMessages(textContact); messageList.remove(position); listViewAdapter.notifyDataSetChanged(); } }; protected void cancelTaskByContactNumber(String textNumber) { // Get message with same conatct number from DB messageDAO = new MessageDAO(this); messageDAO.open(); List<Message> msgList = null; msgList = messageDAO.getMessagesByContactNumber(textNumber); messageDAO.close(); // Cancel Alarm by request code AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, AlarmReceiver.class); for (Message msg : msgList) { int requestCode = parseCalendar(msg.getTextWhen()); Log.d("requstcode in main -->", requestCode + ""); PendingIntent sender = PendingIntent.getBroadcast(this, requestCode, intent, PendingIntent.FLAG_NO_CREATE); if (sender != null) { Log.i("ALARM " + requestCode + " CANCEL", "cancel alarm"); am.cancel(sender); } else { Log.i("ALARM " + requestCode + " CANCEL", "sender == null"); } } } // Parse date return Calendar TimeInMillis public int parseCalendar(String date) { Calendar cal = Calendar.getInstance(); date = date.replace(".", "#"); date = date.replace(" ", "#"); date = date.replace(":", "#"); String[] dateArray = date.split("#"); if (dateArray.length == 5) { cal.set(Calendar.YEAR, new Integer(dateArray[0])); cal.set(Calendar.MONTH, new Integer(dateArray[1]) - 1); cal.set(Calendar.DAY_OF_MONTH, new Integer(dateArray[2])); cal.set(Calendar.HOUR_OF_DAY, new Integer(dateArray[3])); cal.set(Calendar.MINUTE, new Integer(dateArray[4])); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); } return (int) cal.getTimeInMillis(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { finish(); } return super.onKeyDown(keyCode, event); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.newButton: Intent intent = new Intent(MainActivity.this, NewActivity.class); startActivity(intent); break; case R.id.editButton: String tag = (String) editButton.getTag(); if ("edit".equals(tag)) { editButton.setTag("done"); editButton.setImageResource(R.drawable.done); showDeleteLayout(); newButton.setVisibility(View.INVISIBLE); findViewById(R.id.border2).setVisibility(View.INVISIBLE); } if ("done".equals(tag)) { editButton.setTag("edit"); editButton.setImageResource(R.drawable.edit); showDefaultLayout(); newButton.setVisibility(View.VISIBLE); findViewById(R.id.border2).setVisibility(View.VISIBLE); } break; } } @Override protected void onDestroy() { if (adView != null) { adView.destroy(); } super.onDestroy(); } }