Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.mycaller;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import java.util.ArrayList;
- //CatsMan - Categories Management
- public class CatsMan extends AppCompatActivity {
- Context mContext;
- ListView mLvCats; //whe the data is to be displayed
- ArrayList<String> mAlCats; //the runtime data
- ArrayAdapter<String> mAd; //the broker
- CategoriesDB mCatsDB; //the non-volatile SQLite data
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_cats_man);
- init();
- }//onCreate
- void init(){
- mContext = this;
- mLvCats = findViewById(R.id.idLvCats);
- //we want to populate this ListView with the
- //existing (in database) categories
- mCatsDB = new CategoriesDB(mContext);
- //mAlCats = new ArrayList<>();
- mAlCats = mCatsDB.selectAllCats();
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mAlCats
- );
- mLvCats.setAdapter(mAd);
- }//init
- }//CatsMan
- //
- package com.joythis.android.mycaller;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import android.Manifest;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import java.util.ArrayList;
- import java.util.Map;
- public class MainActivity extends AppCompatActivity {
- //1
- public static String[] NECESSARY_PERMISSIONS = {
- Manifest.permission.CALL_PHONE
- };
- ListView.OnItemLongClickListener
- mLvItemLongClickHandler = new ListView.OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick(
- AdapterView<?> parent,
- View view, //is the view in the ListView on which I long-clicked
- int position, //the index of the corresponding contact in mAlContacts
- long id)
- {
- Contact c = mAlContacts.get(position);
- String strName = c.getmName();
- String strNumber = c.getmNumber();
- mUtil.fb("will call to "+strNumber);
- mUtil.phoneTo(strNumber);
- //return false; //false - the event was NOT fully processed
- return true; //true - the event was fully processed (a click listener would NOT happen)
- }//onItemLongClick
- };
- Button.OnClickListener mBtnClickHandler = new Button.OnClickListener() {
- @Override
- public void onClick(View pTheObjectThatWasClicked) {
- switch(pTheObjectThatWasClicked.getId()){
- case R.id.idBtnInsertContact:
- actionInsertContact();
- break;
- case R.id.idBtnContactNumber:
- Button theClickedButton = (Button)pTheObjectThatWasClicked;
- String strNumberInTheButton =
- theClickedButton.getText().toString();
- mUtil.fb("Will call " + strNumberInTheButton);
- //start a phone call to the number referenced in the Button
- mUtil.phoneTo(strNumberInTheButton);
- break;
- }//switch
- }//onClick
- };//mBtnClickHandler
- void actionInsertContact(){
- String strContactName = mEtContactName.getText().toString();
- String strContactNumber = mEtContactNumber.getText().toString();
- Contact c = new Contact(
- strContactName,
- strContactNumber
- );
- long iWhereItWasInserted = mContactDB.insertContact(c);
- /*
- Toast t = Toast.makeText(
- this,
- String.valueOf(iWhereItWasInserted),
- Toast.LENGTH_LONG
- );
- t.show();
- */
- mUtil.fb(
- String.valueOf(iWhereItWasInserted)
- );
- syncLvContactsWithDB();
- }//actionInsertContact
- //data member to enable access to database operations
- //such as insert and select
- ContactDB mContactDB;
- CategoriesDB mCatsDB;
- Context mContext;
- AmUtil mUtil;
- EditText mEtContactName, mEtContactNumber;
- Button mBtnInsertContact;
- ListView mLvContacts;
- ArrayList<Contact> mAlContacts;
- ArrayAdapter<Contact> mAd;
- ContactAdapter mAd2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.insert_contact_rl);
- init();
- }//onCreate
- public final static int
- CALL_ME_ON_THIS_CODE_WHEN_THE_USER_ANSWERS_THE_REQUEST
- =
- 123;
- void init(){
- mContext = this;
- mUtil = new AmUtil(this);
- mEtContactName = findViewById(R.id.idEtContactName);
- mEtContactNumber = findViewById(R.id.idEtContactNumber);
- mBtnInsertContact = findViewById(R.id.idBtnInsertContact);
- mBtnInsertContact.setOnClickListener(
- mBtnClickHandler //data abstraction
- );
- mLvContacts = findViewById(R.id.idLvContacts);
- mLvContacts.setOnItemLongClickListener(
- mLvItemLongClickHandler
- );
- mAlContacts = new ArrayList<>();
- mContactDB = new ContactDB(mContext);
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mAlContacts
- );
- mAd2 = new ContactAdapter(
- mContext,
- R.layout.contact_ll,
- mAlContacts,
- //null
- mBtnClickHandler
- );
- //mLvContacts.setAdapter(mAd);
- mLvContacts.setAdapter(mAd2); //TODO: refresh of sort options when using the custom views
- syncLvContactsWithDB();
- /*
- mContactDB.insertContact
- (new Contact("Artur", "123"));
- ArrayList<Contact> all = mContactDB.selectAll();
- */
- mUtil.requestNecessaryPermissionsStillDenied(
- NECESSARY_PERMISSIONS,
- CALL_ME_ON_THIS_CODE_WHEN_THE_USER_ANSWERS_THE_REQUEST
- );
- //2020-12-10
- //related with Categories
- mCatsDB = new CategoriesDB(mContext);
- mCatsDB.insertCategory("WORK");
- mCatsDB.insertCategory("FAMILY");
- mCatsDB.insertCategory("FRIENDS");
- mCatsDB.insertCategory("OTHER");
- }//init
- void syncLvContactsWithDB(){
- ArrayList<Contact> alTemp =
- mContactDB.selectAllByInsertDateDESC();
- if (alTemp!=null && alTemp.size()>0){
- mAlContacts.clear();
- for (Contact c : alTemp){
- mAlContacts.add(c);
- }//for
- mAd.notifyDataSetChanged();
- }//if
- }//syncLvContactsWithDB
- void syncLvContactsWithDB(
- ArrayList<Contact> pContacts
- ){
- if (pContacts!=null && pContacts.size()>0){
- mAlContacts.clear();
- for (Contact c : pContacts){
- mAlContacts.add(c);
- }//for
- //mAd.notifyDataSetChanged();
- mAd2.notifyDataSetChanged();
- }//if
- }//syncLvContactsWithDB
- //attaches the menu to the Activity
- @Override
- public boolean onCreateOptionsMenu(Menu pMenu) {
- MenuInflater minf = new MenuInflater(mContext);
- if (minf!=null){
- minf.inflate(
- R.menu.menu_sort_options,
- pMenu
- );
- }
- return super.onCreateOptionsMenu(pMenu);
- }//onCreateOptionsMenu
- //this sets the menu's behavior
- @Override
- public boolean onOptionsItemSelected(@NonNull MenuItem item) {
- switch(item.getItemId()){
- case R.id.idMenuItemRequestDeniedPermissions:
- //PackageManager.PERMISSION_GRANTED //0
- //PackageManager.PERMISSION_DENIED //-1
- //0 => {"A", "B"}
- //-1 => {vazio}
- Map<Integer, ArrayList<String>> pairGD =
- mUtil.getListsOfGrantedAndDeniedPermissions
- (NECESSARY_PERMISSIONS);
- ArrayList<String> alDenied =
- pairGD.get(PackageManager.PERMISSION_DENIED);
- if (!alDenied.isEmpty()){
- mUtil.requestNecessaryPermissionsStillDenied(
- NECESSARY_PERMISSIONS,
- CALL_ME_ON_THIS_CODE_WHEN_THE_USER_ANSWERS_THE_REQUEST
- );
- }
- else{
- mUtil.fb("Everything needed is already GRANTED!");
- }
- break;
- case R.id.idMenuItemSortIdAsc:
- syncLvContactsWithDB(
- mContactDB.selectAllByInsertDateASC()
- );
- break;
- case R.id.idMenuItemSortIdDesc:
- syncLvContactsWithDB(
- mContactDB.selectAllByInsertDateDESC()
- );
- break;
- case R.id.idMenuItemSortNameAsc:
- syncLvContactsWithDB(
- mContactDB.selectAllByNameASC()
- );
- break;
- case R.id.idMenuItemSortNameDesc:
- syncLvContactsWithDB(
- mContactDB.selectAllByNameDESC()
- );
- break;
- case R.id.idMenuItemSortNumberAsc:
- syncLvContactsWithDB(
- mContactDB.selectAllByNumberASC()
- );
- break;
- case R.id.idMenuItemSortNumberDesc:
- syncLvContactsWithDB(
- mContactDB.selectAllByNumberDESC()
- );
- break;
- case R.id.idMenuItemSeeGrantedAndDenied:
- String strGrantedAndDenied =
- mUtil.getPermissionStatus(
- NECESSARY_PERMISSIONS
- );
- mUtil.fb(strGrantedAndDenied);
- break;
- }//switch
- return super.onOptionsItemSelected(item);
- }//onOptionsItemSelected
- @Override
- public void onRequestPermissionsResult
- (int requestCode, //check if it is our code
- @NonNull String[] permissions, //the requested permissions
- @NonNull int[] grantResults //the parallel results
- ){
- if(requestCode==CALL_ME_ON_THIS_CODE_WHEN_THE_USER_ANSWERS_THE_REQUEST){
- for(int idx=0; idx<permissions.length; idx++){
- String strCurrentPerm = permissions[idx];
- int iUserResponseToTheCurrentPermission = grantResults[idx];
- boolean bUserSaidYeah =
- iUserResponseToTheCurrentPermission ==
- PackageManager.PERMISSION_GRANTED;
- boolean bUserSaidNo =
- iUserResponseToTheCurrentPermission ==
- PackageManager.PERMISSION_DENIED;
- if (bUserSaidYeah){
- mUtil.fb("Thank you for granting "+strCurrentPerm);
- }
- if (bUserSaidNo){
- mUtil.fb("You denied "+strCurrentPerm);
- }
- }//for
- }//if
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- }//onRequestPermissionsResult
- }//MainActivity
- //
- package com.joythis.android.mycaller;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import androidx.annotation.Nullable;
- import java.util.ArrayList;
- public class CategoriesDB extends SQLiteOpenHelper {
- public final static String DB_NAME = "CATS.DB";
- public final static int DB_VERSION = 1;
- public final static String TABLE_CATEGORIES = "tCategories";
- public final static String COL_ID = "cId";
- public final static String COL_NAME = "cName";
- /*
- CREATE TABLE IF NOT EXISTS tCategories(
- cId INTEGER PRIMARY KEY AUTOINCREMENT,
- cName TEXT NOT NULL
- );
- */
- public final static String CREATE_TABLE_CATEGORIES =
- "CREATE TABLE IF NOT EXISTS "+TABLE_CATEGORIES+"(\n"+
- COL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,\n"+
- COL_NAME+" TEXT NOT NULL\n"+
- ");";
- /*
- DROP TABLE IF EXISTS tCategories;
- */
- public final static String DROP_TABLE_CATEGORIES =
- "DROP TABLE IF EXISTS "+TABLE_CATEGORIES+";";
- void install(SQLiteDatabase pDB){
- pDB.execSQL(CREATE_TABLE_CATEGORIES);
- }//install
- void uninstall(SQLiteDatabase pDB){
- pDB.execSQL(DROP_TABLE_CATEGORIES);
- }//uninstall
- public CategoriesDB(@Nullable Context context) {
- super(
- context,
- DB_NAME,
- null,
- DB_VERSION
- );
- }//CategoriesDB
- public CategoriesDB(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
- super(context, name, factory, version);
- }//CategoriesDB
- @Override
- public void onCreate(SQLiteDatabase db) {
- install(db);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- if (newVersion>oldVersion){
- //TODO: backup to TSV file
- uninstall(db);
- install(db);
- }
- }//onUpgrade
- public long insertCategory(
- String pStrName
- ){
- SQLiteDatabase db = getWritableDatabase();
- if (db!=null){
- ContentValues cvs = new ContentValues();
- cvs.put(COL_NAME, pStrName);
- long iWhereInserted =
- db.insert(
- TABLE_CATEGORIES,
- null,
- cvs
- );
- db.close();
- return iWhereInserted;
- }
- return -1;
- }//insertCategory
- public String[] selectAllCats1(){
- //convert from ArrayList<String> to String[]
- ArrayList<String> alCats = selectAllCats();
- return alCats.toArray(new String[alCats.size()]);
- }//selectAllCats1
- public ArrayList<String> selectAllCats(){
- ArrayList<String> alRet = new ArrayList<>();
- SQLiteDatabase db = this.getReadableDatabase();
- if (db!=null){
- Cursor cursor =
- db.rawQuery(
- "SELECT * FROM "+TABLE_CATEGORIES,
- null
- );
- cursor.moveToFirst();
- while(!cursor.isAfterLast()){
- String strCatName = cursor.getString(
- cursor.getColumnIndex(COL_NAME)
- );
- alRet.add(strCatName);
- cursor.moveToNext();
- }//while
- db.close();
- }
- return alRet;
- }
- }//CategoriesDB
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement