diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/AndroidManifest.xml b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/AndroidManifest.xml new file mode 100644 index 00000000..3489dae1 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/AndroidManifest.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Fragment/Net1314080903122_SmsHistory.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Fragment/Net1314080903122_SmsHistory.java new file mode 100644 index 00000000..67505773 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Fragment/Net1314080903122_SmsHistory.java @@ -0,0 +1,116 @@ +package edu.hzuapps.androidworks.homeworks.net1314080903122.Fragment; + + +import android.content.Context; +import android.database.Cursor; +import android.os.Bundle; +import android.support.v4.app.ListFragment; +import android.support.v4.app.LoaderManager; +import android.support.v4.content.CursorLoader; +import android.support.v4.content.Loader; +import android.support.v4.widget.CursorAdapter; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +import edu.hzuapps.androidworks.homeworks.net1314080903122.R; +import edu.hzuapps.androidworks.homeworks.net1314080903122.bean.Net1314080903122_SendedMsg; +import edu.hzuapps.androidworks.homeworks.net1314080903122.db.Net1314080903122_SmsProvider; +import edu.hzuapps.androidworks.homeworks.net1314080903122.view.FlowLayout; + +/** + * Created by Administrator on 2016/5/29. + */ +public class Net1314080903122_SmsHistory extends ListFragment{ + private static final int LOADER_ID = 1; + private LayoutInflater mInflater; + private CursorAdapter mCursorAdapter; + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + mInflater = LayoutInflater.from(getActivity()); + initLoader(); + setupListAdapter(); + } + + private void setupListAdapter() { + mCursorAdapter = new CursorAdapter(getActivity(),null,false) { + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + View view = mInflater.inflate(R.layout.net1314080903122_item_sended_msg, parent, false); + return view; + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + TextView msg = (TextView) view.findViewById(R.id.id_tv_msg); + FlowLayout fl = (FlowLayout) view.findViewById(R.id.id_fl_contacts); + TextView fes = (TextView) view.findViewById(R.id.id_tv_fes); + TextView date = (TextView) view.findViewById(R.id.id_tv_date); + + msg.setText(cursor.getString(cursor.getColumnIndex(Net1314080903122_SendedMsg.COLUMN_MSG))); + fes.setText(cursor.getString(cursor.getColumnIndex(Net1314080903122_SendedMsg.COLUMN_FES_NAME))); + long dateVal=cursor.getLong(cursor.getColumnIndex(Net1314080903122_SendedMsg.COLUMN_DATE)); + date.setText(parseDate(dateVal)); + String names = cursor.getString(cursor.getColumnIndex(Net1314080903122_SendedMsg.COLUMN_NAMES)); + if (TextUtils.isEmpty(names)) + { + return; + } + fl.removeAllViews(); + for (String name : names.split(":")) + { + addTag(name,fl); + } + } + }; + setListAdapter(mCursorAdapter); + } + + DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm"); + private String parseDate(long dateVal) { + return df.format(dateVal); + } + + + + private void addTag(String name, FlowLayout fl) { + TextView tv = (TextView) mInflater.inflate(R.layout.net1314080903122_tag, fl, false); + tv.setText(name); + fl.addView(tv); + } + + + + + + private void initLoader() { + getLoaderManager().initLoader(LOADER_ID, null,new LoaderManager.LoaderCallbacks() { + @Override + public Loader onCreateLoader(int id, Bundle args) { + CursorLoader loader = new CursorLoader(getActivity(), Net1314080903122_SmsProvider.URI_SMS_ALL,null,null,null,null); + return loader; + } + + @Override + public void onLoadFinished(Loader loader, Cursor data) { + if (loader.getId() == LOADER_ID) + { + mCursorAdapter.swapCursor(data); + } + } + + @Override + public void onLoaderReset(Loader loader) { + mCursorAdapter.swapCursor(null); + + } + }); + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Net1314080903122_MainActivity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Net1314080903122_MainActivity.java index 24fa6603..7aba04ec 100644 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Net1314080903122_MainActivity.java +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Net1314080903122_MainActivity.java @@ -14,6 +14,7 @@ import android.view.MenuItem; import edu.hzuapps.androidworks.homeworks.net1314080903122.Fragment.Net1314080903122_Category; +import edu.hzuapps.androidworks.homeworks.net1314080903122.Fragment.Net1314080903122_SmsHistory; public class Net1314080903122_MainActivity extends AppCompatActivity { @@ -41,6 +42,7 @@ private void initViews() { { @Override public Fragment getItem(int position) { + if (position == 1)return new Net1314080903122_SmsHistory(); return new Net1314080903122_Category(); } diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Net1314080903122_SendMsg.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Net1314080903122_SendMsg.java index b8121d69..4c7704da 100644 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Net1314080903122_SendMsg.java +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/Net1314080903122_SendMsg.java @@ -1,25 +1,42 @@ package edu.hzuapps.androidworks.homeworks.net1314080903122; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; +import android.database.Cursor; +import android.net.Uri; import android.os.Bundle; +import android.provider.ContactsContract; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.text.TextUtils; +import android.util.Log; +import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + +import java.util.HashSet; import edu.hzuapps.androidworks.homeworks.net1314080903122.R; import edu.hzuapps.androidworks.homeworks.net1314080903122.bean.Net1314080903122_Festival; import edu.hzuapps.androidworks.homeworks.net1314080903122.bean.Net1314080903122_FestivalLab; import edu.hzuapps.androidworks.homeworks.net1314080903122.bean.Net1314080903122_Msg; +import edu.hzuapps.androidworks.homeworks.net1314080903122.bean.Net1314080903122_SendedMsg; +import edu.hzuapps.androidworks.homeworks.net1314080903122.biz.Net1314080903122_SmsBiz; +import edu.hzuapps.androidworks.homeworks.net1314080903122.view.FlowLayout; public class Net1314080903122_SendMsg extends AppCompatActivity { public static final String KEY_FESTIVAL_ID = "festivalId"; public static final String KEY_MSG_ID = "msgId"; + private static final int CODE_REQUEST = 1; private int mFstivalId; private int msgId; @@ -28,30 +45,145 @@ public class Net1314080903122_SendMsg extends AppCompatActivity { private EditText mEdMsg; private Button mBtnAdd; + private FlowLayout mFlContacts; private FloatingActionButton mFabSend; private View mLayoutLoading; + private HashSet mContactNames = new HashSet<>(); + private HashSet mContactNums = new HashSet<>(); + + private LayoutInflater mInflater; + + public static final String ACTION_SEND_MSG = "ACTION_SEND_MSG"; + public static final String ACTION_DELIVER_MSG = "ACTION_DELIVER_MSG"; + + private PendingIntent mSendPi; + private PendingIntent mDeliverPi; + private BroadcastReceiver mSendBroadcastReceiver; + private BroadcastReceiver mDeliverBroadcastReceiver; + + private Net1314080903122_SmsBiz mSmsBiz; + + private int mMsgSendCount; + private int mTotalCount; + + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_net1314080903122__send_msg); + mInflater = LayoutInflater.from(this); + + mSmsBiz = new Net1314080903122_SmsBiz(this); + initDatas(); initView(); + initEvents(); + initRecivers(); + + } + + private void initRecivers() { + Intent sendIntent = new Intent(ACTION_SEND_MSG); + mSendPi = PendingIntent.getBroadcast(this,0,sendIntent,0); + Intent deliverIntent = new Intent(ACTION_DELIVER_MSG); + mDeliverPi = PendingIntent.getBroadcast(this,0,deliverIntent,0); + + registerReceiver(mSendBroadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (getResultCode() == RESULT_OK) { + Log.e("TAG", "短信发送成功" + (mMsgSendCount + "/" + mTotalCount)); + } else { + Log.e("TAG", "短信发送失败"); + } + Toast.makeText(Net1314080903122_SendMsg.this, (mMsgSendCount + "/" + mTotalCount) + "短信发送成功", Toast.LENGTH_SHORT).show(); + mMsgSendCount++; + if (mMsgSendCount == mTotalCount) { + finish(); + } + } + }, new IntentFilter(ACTION_SEND_MSG)); + + registerReceiver(mDeliverBroadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + Log.e("TAG", "联系人已经成功接收到我们的短信"); + } + }, new IntentFilter(ACTION_DELIVER_MSG)); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + unregisterReceiver(mSendBroadcastReceiver); + unregisterReceiver(mDeliverBroadcastReceiver); + } + + private void initEvents() { + mBtnAdd.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); + startActivityForResult(intent, CODE_REQUEST); +// TextView vvieww = (TextView) mInflater.inflate(R.layout.net1314080903122_tag, mFlContacts, false); +// mFlContacts.addView(vvieww); + } + }); + mFabSend.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mContactNames.size() == 0) { + Toast.makeText(Net1314080903122_SendMsg.this, "请先选择联系人", Toast.LENGTH_SHORT).show(); + return; + } + String msg = mEdMsg.getText().toString(); + if (TextUtils.isEmpty(msg)) { + Toast.makeText(Net1314080903122_SendMsg.this, "短信内容不能为空", Toast.LENGTH_SHORT).show(); + return; + } + mLayoutLoading.setVisibility(View.VISIBLE); + mTotalCount = mSmsBiz.sendMsg(mContactNums, buildSendMsg(msg), mSendPi, mDeliverPi); + mMsgSendCount = 0; + } + }); + } + private Net1314080903122_SendedMsg buildSendMsg(String msg) { + Net1314080903122_SendedMsg sendedMsg = new Net1314080903122_SendedMsg(); + sendedMsg.setMsg(msg); + sendedMsg.setFestivalName(mFestival.getName()); + String names =""; + for (String name :mContactNames) + { + names += name + ":"; + } + String numbers =""; + for (String number :mContactNums) + { + numbers += numbers + ":"; + } + sendedMsg.setNames(names.substring(0, names.length() - 1)); + sendedMsg.setNumbers(numbers.substring(0, numbers.length() - 1)); + return sendedMsg; + } + + private void initView() { mEdMsg = (EditText) findViewById(R.id.id_et_content); mBtnAdd = (Button) findViewById(R.id.id_btn_add); + mFlContacts= (FlowLayout) findViewById(R.id.id_fl_contacts); mFabSend = (FloatingActionButton) findViewById(R.id.id_fab_send); mLayoutLoading = findViewById(R.id.id_layout_loading); mLayoutLoading.setVisibility(View.GONE); - mFabSend.setOnClickListener(new View.OnClickListener() { + /* mFabSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mLayoutLoading.getVisibility() == View.GONE) ; @@ -59,11 +191,11 @@ public void onClick(View v) { mLayoutLoading.setVisibility(View.VISIBLE); } } - }); + }); + */ - - if (msgId !=-1) + if (msgId !=-1) { mMsg = Net1314080903122_FestivalLab.getInstance().getMsgByMsgId(msgId); mEdMsg.setText(mMsg.getContent()); @@ -78,11 +210,60 @@ private void initDatas() { setTitle(mFestival.getName()); } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == CODE_REQUEST) + { + if (resultCode == RESULT_OK) + { + Uri contactUri=data.getData(); + Cursor cursor = getContentResolver().query(contactUri, null, null, null, null); + cursor.moveToFirst(); + String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); + + String number = getContactNumber(cursor); + if (!TextUtils.isEmpty(number)) + { + mContactNums.add(number); + mContactNames.add(contactName); + addTag(contactName); + + } + + + } + + } + } + + private void addTag(String contactName) { + TextView view = (TextView)mInflater.inflate(R.layout.net1314080903122_tag,mFlContacts,false); + view.setText(contactName); + mFlContacts.addView(view); + + + } + + private String getContactNumber(Cursor cursor) { + int numberCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); + String number =null; + if (numberCount > 0) + { + int contactId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID)); + Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); + phoneCursor.moveToFirst(); + number = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); + phoneCursor.close(); + } + cursor.close(); + return number; + } public static void toActivity(Context context,int festivalId,int msgId){ Intent intent = new Intent(context,Net1314080903122_SendMsg.class); intent.putExtra(KEY_FESTIVAL_ID,festivalId); - intent.putExtra(KEY_MSG_ID,msgId); + intent.putExtra(KEY_MSG_ID, msgId); context.startActivity(intent); } diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/bean/Net1314080903122_SendedMsg.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/bean/Net1314080903122_SendedMsg.java new file mode 100644 index 00000000..b065b30f --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/bean/Net1314080903122_SendedMsg.java @@ -0,0 +1,82 @@ +package edu.hzuapps.androidworks.homeworks.net1314080903122.bean; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by Administrator on 2016/5/29. + */ +public class Net1314080903122_SendedMsg { + private int id; + private String msg; + private String numbers; + private String names; + private String festivalName; + private Date date; + private String dateStr; + private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + + public static final String TABLE_NAME = "tb_sended_msg"; + public static final String COLUMN_MSG = "msg"; + public static final String COLUMN_NUMBER = "numbers"; + public static final String COLUMN_NAMES = "names"; + public static final String COLUMN_FES_NAME = "festivalName"; + public static final String COLUMN_DATE = "date"; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public String getNumbers() { + return numbers; + } + + public void setNumbers(String numbers) { + this.numbers = numbers; + } + + public String getNames() { + return names; + } + + public void setNames(String names) { + this.names = names; + } + + public String getFestivalName() { + return festivalName; + } + + public void setFestivalName(String festivalName) { + this.festivalName = festivalName; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public String getDateStr() { + return dateStr; + } + + public void setDateStr(String dateStr) { + this.dateStr = dateStr; + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/biz/Net1314080903122_SmsBiz.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/biz/Net1314080903122_SmsBiz.java new file mode 100644 index 00000000..dcb05942 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/biz/Net1314080903122_SmsBiz.java @@ -0,0 +1,62 @@ +package edu.hzuapps.androidworks.homeworks.net1314080903122.biz; + +import android.app.PendingIntent; +import android.content.ContentValues; +import android.content.Context; +import android.telephony.SmsManager; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Set; + +import edu.hzuapps.androidworks.homeworks.net1314080903122.bean.Net1314080903122_SendedMsg; +import edu.hzuapps.androidworks.homeworks.net1314080903122.db.Net1314080903122_SmsProvider; + +/** + * Created by Administrator on 2016/5/27. + */ +public class Net1314080903122_SmsBiz { + private Context context; + public Net1314080903122_SmsBiz(Context context) + { + this.context = context; + } + public int sendMsg(String number,String msg,PendingIntent sentPi,PendingIntent deliverPi) + { + SmsManager smsManager = SmsManager.getDefault(); + ArrayList contents = smsManager.divideMessage(msg); + for (String content : contents) + { + smsManager.sendTextMessage(number,null,content,sentPi,deliverPi); + + } + return contents.size(); + } + + public int sendMsg(Set numbers,Net1314080903122_SendedMsg msg,PendingIntent sentPi,PendingIntent deliverPi) + { + save(msg); + int result = 0; + for (String number : numbers) + { + int count = sendMsg(number,msg.getMsg(),sentPi,deliverPi); + result += count; + } + return result; + } + + private void save(Net1314080903122_SendedMsg sendedMsg) + { + sendedMsg.setDate(new Date()); + ContentValues values = new ContentValues(); + values.put(Net1314080903122_SendedMsg.COLUMN_DATE,sendedMsg.getDate().getTime()); + values.put(Net1314080903122_SendedMsg.COLUMN_FES_NAME,sendedMsg.getFestivalName()); + values.put(Net1314080903122_SendedMsg.COLUMN_MSG,sendedMsg.getMsg()); + values.put(Net1314080903122_SendedMsg.COLUMN_NAMES,sendedMsg.getNames()); + values.put(Net1314080903122_SendedMsg.COLUMN_NUMBER,sendedMsg.getNumbers()); + + context.getContentResolver().insert(Net1314080903122_SmsProvider.URI_SMS_ALL,values); + + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/db/Net1314080903122_SmsDbOpenHelper.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/db/Net1314080903122_SmsDbOpenHelper.java new file mode 100644 index 00000000..abf82482 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/db/Net1314080903122_SmsDbOpenHelper.java @@ -0,0 +1,55 @@ +package edu.hzuapps.androidworks.homeworks.net1314080903122.db; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +import edu.hzuapps.androidworks.homeworks.net1314080903122.bean.Net1314080903122_SendedMsg; + +/** + * Created by Administrator on 2016/5/29. + */ +public class Net1314080903122_SmsDbOpenHelper extends SQLiteOpenHelper{ + private static final String DB_NAME ="sms.db"; + private static final int DB_VERSION =1; + + + public Net1314080903122_SmsDbOpenHelper(Context context) { + super(context.getApplicationContext(), DB_NAME, null, DB_VERSION); + } + + private static Net1314080903122_SmsDbOpenHelper mHelper; + public static Net1314080903122_SmsDbOpenHelper getInstance(Context context) + { + if (mHelper == null) + { + synchronized (Net1314080903122_SmsDbOpenHelper.class) + { + if (mHelper == null) + { + mHelper = new Net1314080903122_SmsDbOpenHelper(context); + } + } + } + return mHelper; + } + + + @Override + public void onCreate(SQLiteDatabase db) { + String sql = "create table " + Net1314080903122_SendedMsg.TABLE_NAME +" ( "+ + "_id integer primary key autoincrement ,"+ + Net1314080903122_SendedMsg.COLUMN_DATE+" integer ,"+ + Net1314080903122_SendedMsg.COLUMN_FES_NAME +" text ,"+ + Net1314080903122_SendedMsg.COLUMN_MSG +" text ,"+ + Net1314080903122_SendedMsg.COLUMN_NAMES +" text ,"+ + Net1314080903122_SendedMsg.COLUMN_NUMBER +" text "+ + " ) "; + db.execSQL(sql); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/db/Net1314080903122_SmsProvider.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/db/Net1314080903122_SmsProvider.java new file mode 100644 index 00000000..ce2130ec --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/db/Net1314080903122_SmsProvider.java @@ -0,0 +1,103 @@ +package edu.hzuapps.androidworks.homeworks.net1314080903122.db; + +import android.content.ContentProvider; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.UriMatcher; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.net.Uri; +import android.support.annotation.Nullable; + +import edu.hzuapps.androidworks.homeworks.net1314080903122.bean.Net1314080903122_SendedMsg; + +/** + * Created by Administrator on 2016/5/29. + */ +public class Net1314080903122_SmsProvider extends ContentProvider { + + private static final String AUTHORITY = "edu.hzuapps.androidworks.homeworks.sms.provider.SmsProvider"; + public static final Uri URI_SMS_ALL = Uri.parse("content://"+AUTHORITY+"/sms"); + + private static UriMatcher matcher; + + private static final int SMS_ALL = 0; + private static final int SMS_ONE = 1; + + static { + matcher = new UriMatcher(UriMatcher.NO_MATCH); + matcher.addURI(AUTHORITY,"sms",SMS_ALL); + matcher.addURI(AUTHORITY,"sms/#",SMS_ONE); + } + + private Net1314080903122_SmsDbOpenHelper mHelper; + private SQLiteDatabase mDb; + + + @Override + public boolean onCreate() { + mHelper = Net1314080903122_SmsDbOpenHelper.getInstance(getContext()); + return true; + } + + @Nullable + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { + int match =matcher.match(uri); + switch(match) + { + case SMS_ALL: + break; + case SMS_ONE: + long id = ContentUris.parseId(uri); + selection = "_id = ?"; + selectionArgs = new String[]{String.valueOf(id)}; + break; + + default: + throw new IllegalArgumentException("Wrong URI ; " + uri); + } + mDb = mHelper.getReadableDatabase(); + Cursor c = mDb.query(Net1314080903122_SendedMsg.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); + c.setNotificationUri(getContext().getContentResolver(),URI_SMS_ALL); + return c; + } + + @Nullable + @Override + public String getType(Uri uri) { + return null; + } + + @Nullable + @Override + public Uri insert(Uri uri, ContentValues values) { + int match = matcher.match(uri); + if (match != SMS_ALL ) + { + throw new IllegalArgumentException("Wrong URI ; " + uri); + } + mDb = mHelper.getWritableDatabase(); + long rowID = mDb.insert(Net1314080903122_SendedMsg.TABLE_NAME,null,values); + if (rowID > 0) + { + notifyDataSetChanged(); + return ContentUris.withAppendedId(uri,rowID); + } + return null; + } + + private void notifyDataSetChanged() { + getContext().getContentResolver().notifyChange(URI_SMS_ALL,null); + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + return 0; + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + return 0; + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/view/FlowLayout.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/view/FlowLayout.java new file mode 100644 index 00000000..f1d3145f --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903122/view/FlowLayout.java @@ -0,0 +1,180 @@ +package edu.hzuapps.androidworks.homeworks.net1314080903122.view; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; + +import java.util.ArrayList; +import java.util.List; + +public class FlowLayout extends ViewGroup +{ + + public FlowLayout(Context context, AttributeSet attrs, int defStyle) + { + super(context, attrs, defStyle); + // + } + + public FlowLayout(Context context, AttributeSet attrs) + { + this(context, attrs, 0); + } + + public FlowLayout(Context context) + { + this(context, null); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) + { + int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); + int modeWidth = MeasureSpec.getMode(widthMeasureSpec); + int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); + int modeHeight = MeasureSpec.getMode(heightMeasureSpec); + + // wrap_content + int width = 0; + int height = 0; + + int lineWidth = 0; + int lineHeight = 0; + + int cCount = getChildCount(); + + for (int i = 0; i < cCount; i++) + { + View child = getChildAt(i); + measureChild(child, widthMeasureSpec, heightMeasureSpec); + MarginLayoutParams lp = (MarginLayoutParams) child + .getLayoutParams(); + + int childWidth = child.getMeasuredWidth() + lp.leftMargin + + lp.rightMargin; + int childHeight = child.getMeasuredHeight() + lp.topMargin + + lp.bottomMargin; + + if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) + { + width = Math.max(width, lineWidth); + lineWidth = childWidth; + height += lineHeight; + lineHeight = childHeight; + } else + { + lineWidth += childWidth; + lineHeight = Math.max(lineHeight, childHeight); + } + if (i == cCount - 1) + { + width = Math.max(lineWidth, width); + height += lineHeight; + } + } + + + setMeasuredDimension( + // + modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width + getPaddingLeft() + getPaddingRight(), + modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height + getPaddingTop()+ getPaddingBottom()// + ); + + } + + /** + */ + private List> mAllViews = new ArrayList>(); + /** + */ + private List mLineHeight = new ArrayList(); + + @Override + protected void onLayout(boolean changed, int l, int t, int r, int b) + { + mAllViews.clear(); + mLineHeight.clear(); + + int width = getWidth(); + + int lineWidth = 0; + int lineHeight = 0; + + List lineViews = new ArrayList(); + + int cCount = getChildCount(); + + for (int i = 0; i < cCount; i++) + { + View child = getChildAt(i); + MarginLayoutParams lp = (MarginLayoutParams) child + .getLayoutParams(); + + int childWidth = child.getMeasuredWidth(); + int childHeight = child.getMeasuredHeight(); + + if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width - getPaddingLeft() - getPaddingRight()) + { + mLineHeight.add(lineHeight); + mAllViews.add(lineViews); + + lineWidth = 0; + lineHeight = childHeight + lp.topMargin + lp.bottomMargin; + lineViews = new ArrayList(); + } + lineWidth += childWidth + lp.leftMargin + lp.rightMargin; + lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + + lp.bottomMargin); + lineViews.add(child); + + }// for end + mLineHeight.add(lineHeight); + mAllViews.add(lineViews); + + + int left = getPaddingLeft(); + int top = getPaddingTop(); + + int lineNum = mAllViews.size(); + + for (int i = 0; i < lineNum; i++) + { + lineViews = mAllViews.get(i); + lineHeight = mLineHeight.get(i); + + for (int j = 0; j < lineViews.size(); j++) + { + View child = lineViews.get(j); + if (child.getVisibility() == View.GONE) + { + continue; + } + + MarginLayoutParams lp = (MarginLayoutParams) child + .getLayoutParams(); + + int lc = left + lp.leftMargin; + int tc = top + lp.topMargin; + int rc = lc + child.getMeasuredWidth(); + int bc = tc + child.getMeasuredHeight(); + child.layout(lc, tc, rc, bc); + + left += child.getMeasuredWidth() + lp.leftMargin + + lp.rightMargin; + } + left = getPaddingLeft() ; + top += lineHeight ; + } + + } + + /** + */ + @Override + public LayoutParams generateLayoutParams(AttributeSet attrs) + { + return new MarginLayoutParams(getContext(), attrs); + } + +} diff --git a/app/src/main/res/drawable-hdpi/net1314080903122_panda_icon.png b/app/src/main/res/drawable-hdpi/net1314080903122_panda_icon.png new file mode 100644 index 00000000..bf867ee8 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/net1314080903122_panda_icon.png differ diff --git a/app/src/main/res/drawable/net1314080903122_tag_bg.xml b/app/src/main/res/drawable/net1314080903122_tag_bg.xml new file mode 100644 index 00000000..7fb7fd7f --- /dev/null +++ b/app/src/main/res/drawable/net1314080903122_tag_bg.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_net1314080903122__send_msg.xml b/app/src/main/res/layout/activity_net1314080903122__send_msg.xml index 4adffd61..5b964f57 100644 --- a/app/src/main/res/layout/activity_net1314080903122__send_msg.xml +++ b/app/src/main/res/layout/activity_net1314080903122__send_msg.xml @@ -7,14 +7,24 @@ android:paddingBottom="16dp" android:paddingTop="16dp" - tools:context=".Net1314080903122_SendMsg"> + + android:orientation="vertical" + android:layout_gravity="center"> + + + - - + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/net1314080903122_tag.xml b/app/src/main/res/layout/net1314080903122_tag.xml new file mode 100644 index 00000000..650bf31f --- /dev/null +++ b/app/src/main/res/layout/net1314080903122_tag.xml @@ -0,0 +1,11 @@ + + + + \ No newline at end of file