Advertisement
am_dot_com

PDM2 20201210

Dec 10th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.37 KB | None | 0 0
  1. package com.joythis.android.mycaller;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.ListView;
  9.  
  10. import java.util.ArrayList;
  11.  
  12. //CatsMan - Categories Management
  13. public class CatsMan extends AppCompatActivity {
  14. Context mContext;
  15.  
  16. ListView mLvCats; //whe the data is to be displayed
  17. ArrayList<String> mAlCats; //the runtime data
  18. ArrayAdapter<String> mAd; //the broker
  19. CategoriesDB mCatsDB; //the non-volatile SQLite data
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_cats_man);
  25.  
  26. init();
  27. }//onCreate
  28.  
  29. void init(){
  30. mContext = this;
  31.  
  32. mLvCats = findViewById(R.id.idLvCats);
  33.  
  34. //we want to populate this ListView with the
  35. //existing (in database) categories
  36. mCatsDB = new CategoriesDB(mContext);
  37. //mAlCats = new ArrayList<>();
  38. mAlCats = mCatsDB.selectAllCats();
  39. mAd = new ArrayAdapter<>(
  40. mContext,
  41. android.R.layout.simple_list_item_1,
  42. mAlCats
  43. );
  44. mLvCats.setAdapter(mAd);
  45.  
  46. }//init
  47. }//CatsMan
  48.  
  49. //
  50.  
  51. package com.joythis.android.mycaller;
  52.  
  53. import androidx.annotation.NonNull;
  54. import androidx.appcompat.app.AppCompatActivity;
  55.  
  56. import android.Manifest;
  57. import android.content.Context;
  58. import android.content.pm.PackageManager;
  59. import android.os.Bundle;
  60. import android.view.Menu;
  61. import android.view.MenuInflater;
  62. import android.view.MenuItem;
  63. import android.view.View;
  64. import android.widget.AdapterView;
  65. import android.widget.ArrayAdapter;
  66. import android.widget.Button;
  67. import android.widget.EditText;
  68. import android.widget.ListView;
  69.  
  70. import java.util.ArrayList;
  71. import java.util.Map;
  72.  
  73. public class MainActivity extends AppCompatActivity {
  74.  
  75. //1
  76. public static String[] NECESSARY_PERMISSIONS = {
  77. Manifest.permission.CALL_PHONE
  78. };
  79.  
  80. ListView.OnItemLongClickListener
  81. mLvItemLongClickHandler = new ListView.OnItemLongClickListener() {
  82. @Override
  83. public boolean onItemLongClick(
  84. AdapterView<?> parent,
  85. View view, //is the view in the ListView on which I long-clicked
  86. int position, //the index of the corresponding contact in mAlContacts
  87. long id)
  88. {
  89. Contact c = mAlContacts.get(position);
  90. String strName = c.getmName();
  91. String strNumber = c.getmNumber();
  92. mUtil.fb("will call to "+strNumber);
  93.  
  94. mUtil.phoneTo(strNumber);
  95.  
  96. //return false; //false - the event was NOT fully processed
  97. return true; //true - the event was fully processed (a click listener would NOT happen)
  98. }//onItemLongClick
  99. };
  100.  
  101. Button.OnClickListener mBtnClickHandler = new Button.OnClickListener() {
  102. @Override
  103. public void onClick(View pTheObjectThatWasClicked) {
  104. switch(pTheObjectThatWasClicked.getId()){
  105. case R.id.idBtnInsertContact:
  106. actionInsertContact();
  107. break;
  108.  
  109. case R.id.idBtnContactNumber:
  110. Button theClickedButton = (Button)pTheObjectThatWasClicked;
  111.  
  112. String strNumberInTheButton =
  113. theClickedButton.getText().toString();
  114. mUtil.fb("Will call " + strNumberInTheButton);
  115. //start a phone call to the number referenced in the Button
  116. mUtil.phoneTo(strNumberInTheButton);
  117.  
  118. break;
  119. }//switch
  120. }//onClick
  121. };//mBtnClickHandler
  122.  
  123. void actionInsertContact(){
  124. String strContactName = mEtContactName.getText().toString();
  125. String strContactNumber = mEtContactNumber.getText().toString();
  126.  
  127. Contact c = new Contact(
  128. strContactName,
  129. strContactNumber
  130. );
  131.  
  132. long iWhereItWasInserted = mContactDB.insertContact(c);
  133.  
  134. /*
  135. Toast t = Toast.makeText(
  136. this,
  137. String.valueOf(iWhereItWasInserted),
  138. Toast.LENGTH_LONG
  139. );
  140. t.show();
  141. */
  142. mUtil.fb(
  143. String.valueOf(iWhereItWasInserted)
  144. );
  145.  
  146. syncLvContactsWithDB();
  147. }//actionInsertContact
  148.  
  149. //data member to enable access to database operations
  150. //such as insert and select
  151. ContactDB mContactDB;
  152. CategoriesDB mCatsDB;
  153.  
  154. Context mContext;
  155.  
  156. AmUtil mUtil;
  157.  
  158. EditText mEtContactName, mEtContactNumber;
  159. Button mBtnInsertContact;
  160. ListView mLvContacts;
  161. ArrayList<Contact> mAlContacts;
  162. ArrayAdapter<Contact> mAd;
  163. ContactAdapter mAd2;
  164.  
  165. @Override
  166. protected void onCreate(Bundle savedInstanceState) {
  167. super.onCreate(savedInstanceState);
  168. setContentView(R.layout.insert_contact_rl);
  169.  
  170. init();
  171. }//onCreate
  172.  
  173.  
  174. public final static int
  175. CALL_ME_ON_THIS_CODE_WHEN_THE_USER_ANSWERS_THE_REQUEST
  176. =
  177. 123;
  178.  
  179. void init(){
  180. mContext = this;
  181. mUtil = new AmUtil(this);
  182.  
  183. mEtContactName = findViewById(R.id.idEtContactName);
  184. mEtContactNumber = findViewById(R.id.idEtContactNumber);
  185.  
  186. mBtnInsertContact = findViewById(R.id.idBtnInsertContact);
  187. mBtnInsertContact.setOnClickListener(
  188. mBtnClickHandler //data abstraction
  189. );
  190.  
  191. mLvContacts = findViewById(R.id.idLvContacts);
  192. mLvContacts.setOnItemLongClickListener(
  193. mLvItemLongClickHandler
  194. );
  195.  
  196. mAlContacts = new ArrayList<>();
  197.  
  198. mContactDB = new ContactDB(mContext);
  199.  
  200. mAd = new ArrayAdapter<>(
  201. mContext,
  202. android.R.layout.simple_list_item_1,
  203. mAlContacts
  204. );
  205.  
  206. mAd2 = new ContactAdapter(
  207. mContext,
  208. R.layout.contact_ll,
  209. mAlContacts,
  210. //null
  211. mBtnClickHandler
  212. );
  213.  
  214. //mLvContacts.setAdapter(mAd);
  215. mLvContacts.setAdapter(mAd2); //TODO: refresh of sort options when using the custom views
  216.  
  217. syncLvContactsWithDB();
  218.  
  219. /*
  220. mContactDB.insertContact
  221. (new Contact("Artur", "123"));
  222.  
  223. ArrayList<Contact> all = mContactDB.selectAll();
  224. */
  225.  
  226. mUtil.requestNecessaryPermissionsStillDenied(
  227. NECESSARY_PERMISSIONS,
  228. CALL_ME_ON_THIS_CODE_WHEN_THE_USER_ANSWERS_THE_REQUEST
  229. );
  230.  
  231. //2020-12-10
  232. //related with Categories
  233. mCatsDB = new CategoriesDB(mContext);
  234. mCatsDB.insertCategory("WORK");
  235. mCatsDB.insertCategory("FAMILY");
  236. mCatsDB.insertCategory("FRIENDS");
  237. mCatsDB.insertCategory("OTHER");
  238. }//init
  239.  
  240. void syncLvContactsWithDB(){
  241. ArrayList<Contact> alTemp =
  242. mContactDB.selectAllByInsertDateDESC();
  243. if (alTemp!=null && alTemp.size()>0){
  244. mAlContacts.clear();
  245. for (Contact c : alTemp){
  246. mAlContacts.add(c);
  247. }//for
  248. mAd.notifyDataSetChanged();
  249. }//if
  250. }//syncLvContactsWithDB
  251.  
  252. void syncLvContactsWithDB(
  253. ArrayList<Contact> pContacts
  254. ){
  255. if (pContacts!=null && pContacts.size()>0){
  256. mAlContacts.clear();
  257. for (Contact c : pContacts){
  258. mAlContacts.add(c);
  259. }//for
  260. //mAd.notifyDataSetChanged();
  261. mAd2.notifyDataSetChanged();
  262. }//if
  263. }//syncLvContactsWithDB
  264.  
  265. //attaches the menu to the Activity
  266. @Override
  267. public boolean onCreateOptionsMenu(Menu pMenu) {
  268. MenuInflater minf = new MenuInflater(mContext);
  269. if (minf!=null){
  270. minf.inflate(
  271. R.menu.menu_sort_options,
  272. pMenu
  273. );
  274. }
  275. return super.onCreateOptionsMenu(pMenu);
  276. }//onCreateOptionsMenu
  277.  
  278. //this sets the menu's behavior
  279. @Override
  280. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  281. switch(item.getItemId()){
  282. case R.id.idMenuItemRequestDeniedPermissions:
  283. //PackageManager.PERMISSION_GRANTED //0
  284. //PackageManager.PERMISSION_DENIED //-1
  285. //0 => {"A", "B"}
  286. //-1 => {vazio}
  287. Map<Integer, ArrayList<String>> pairGD =
  288. mUtil.getListsOfGrantedAndDeniedPermissions
  289. (NECESSARY_PERMISSIONS);
  290. ArrayList<String> alDenied =
  291. pairGD.get(PackageManager.PERMISSION_DENIED);
  292.  
  293. if (!alDenied.isEmpty()){
  294. mUtil.requestNecessaryPermissionsStillDenied(
  295. NECESSARY_PERMISSIONS,
  296. CALL_ME_ON_THIS_CODE_WHEN_THE_USER_ANSWERS_THE_REQUEST
  297. );
  298. }
  299. else{
  300. mUtil.fb("Everything needed is already GRANTED!");
  301. }
  302.  
  303. break;
  304.  
  305. case R.id.idMenuItemSortIdAsc:
  306. syncLvContactsWithDB(
  307. mContactDB.selectAllByInsertDateASC()
  308. );
  309.  
  310. break;
  311. case R.id.idMenuItemSortIdDesc:
  312. syncLvContactsWithDB(
  313. mContactDB.selectAllByInsertDateDESC()
  314. );
  315.  
  316. break;
  317. case R.id.idMenuItemSortNameAsc:
  318. syncLvContactsWithDB(
  319. mContactDB.selectAllByNameASC()
  320. );
  321. break;
  322. case R.id.idMenuItemSortNameDesc:
  323. syncLvContactsWithDB(
  324. mContactDB.selectAllByNameDESC()
  325. );
  326. break;
  327. case R.id.idMenuItemSortNumberAsc:
  328. syncLvContactsWithDB(
  329. mContactDB.selectAllByNumberASC()
  330. );
  331. break;
  332. case R.id.idMenuItemSortNumberDesc:
  333. syncLvContactsWithDB(
  334. mContactDB.selectAllByNumberDESC()
  335. );
  336. break;
  337.  
  338. case R.id.idMenuItemSeeGrantedAndDenied:
  339. String strGrantedAndDenied =
  340. mUtil.getPermissionStatus(
  341. NECESSARY_PERMISSIONS
  342. );
  343. mUtil.fb(strGrantedAndDenied);
  344. break;
  345.  
  346. }//switch
  347. return super.onOptionsItemSelected(item);
  348. }//onOptionsItemSelected
  349.  
  350. @Override
  351. public void onRequestPermissionsResult
  352. (int requestCode, //check if it is our code
  353. @NonNull String[] permissions, //the requested permissions
  354. @NonNull int[] grantResults //the parallel results
  355. ){
  356. if(requestCode==CALL_ME_ON_THIS_CODE_WHEN_THE_USER_ANSWERS_THE_REQUEST){
  357. for(int idx=0; idx<permissions.length; idx++){
  358. String strCurrentPerm = permissions[idx];
  359. int iUserResponseToTheCurrentPermission = grantResults[idx];
  360.  
  361. boolean bUserSaidYeah =
  362. iUserResponseToTheCurrentPermission ==
  363. PackageManager.PERMISSION_GRANTED;
  364.  
  365. boolean bUserSaidNo =
  366. iUserResponseToTheCurrentPermission ==
  367. PackageManager.PERMISSION_DENIED;
  368.  
  369. if (bUserSaidYeah){
  370. mUtil.fb("Thank you for granting "+strCurrentPerm);
  371. }
  372.  
  373. if (bUserSaidNo){
  374. mUtil.fb("You denied "+strCurrentPerm);
  375. }
  376. }//for
  377. }//if
  378.  
  379. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  380. }//onRequestPermissionsResult
  381. }//MainActivity
  382.  
  383. //
  384.  
  385. package com.joythis.android.mycaller;
  386.  
  387. import android.content.ContentValues;
  388. import android.content.Context;
  389. import android.database.Cursor;
  390. import android.database.sqlite.SQLiteDatabase;
  391. import android.database.sqlite.SQLiteOpenHelper;
  392.  
  393. import androidx.annotation.Nullable;
  394.  
  395. import java.util.ArrayList;
  396.  
  397. public class CategoriesDB extends SQLiteOpenHelper {
  398. public final static String DB_NAME = "CATS.DB";
  399. public final static int DB_VERSION = 1;
  400.  
  401. public final static String TABLE_CATEGORIES = "tCategories";
  402. public final static String COL_ID = "cId";
  403. public final static String COL_NAME = "cName";
  404.  
  405. /*
  406. CREATE TABLE IF NOT EXISTS tCategories(
  407. cId INTEGER PRIMARY KEY AUTOINCREMENT,
  408. cName TEXT NOT NULL
  409. );
  410. */
  411. public final static String CREATE_TABLE_CATEGORIES =
  412. "CREATE TABLE IF NOT EXISTS "+TABLE_CATEGORIES+"(\n"+
  413. COL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,\n"+
  414. COL_NAME+" TEXT NOT NULL\n"+
  415. ");";
  416.  
  417. /*
  418. DROP TABLE IF EXISTS tCategories;
  419. */
  420. public final static String DROP_TABLE_CATEGORIES =
  421. "DROP TABLE IF EXISTS "+TABLE_CATEGORIES+";";
  422.  
  423. void install(SQLiteDatabase pDB){
  424. pDB.execSQL(CREATE_TABLE_CATEGORIES);
  425. }//install
  426.  
  427. void uninstall(SQLiteDatabase pDB){
  428. pDB.execSQL(DROP_TABLE_CATEGORIES);
  429. }//uninstall
  430.  
  431. public CategoriesDB(@Nullable Context context) {
  432. super(
  433. context,
  434. DB_NAME,
  435. null,
  436. DB_VERSION
  437. );
  438. }//CategoriesDB
  439.  
  440. public CategoriesDB(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
  441. super(context, name, factory, version);
  442. }//CategoriesDB
  443.  
  444.  
  445. @Override
  446. public void onCreate(SQLiteDatabase db) {
  447. install(db);
  448. }
  449.  
  450. @Override
  451. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  452. if (newVersion>oldVersion){
  453. //TODO: backup to TSV file
  454. uninstall(db);
  455. install(db);
  456. }
  457. }//onUpgrade
  458.  
  459. public long insertCategory(
  460. String pStrName
  461. ){
  462. SQLiteDatabase db = getWritableDatabase();
  463. if (db!=null){
  464. ContentValues cvs = new ContentValues();
  465. cvs.put(COL_NAME, pStrName);
  466. long iWhereInserted =
  467. db.insert(
  468. TABLE_CATEGORIES,
  469. null,
  470. cvs
  471. );
  472. db.close();
  473. return iWhereInserted;
  474. }
  475. return -1;
  476. }//insertCategory
  477.  
  478. public String[] selectAllCats1(){
  479. //convert from ArrayList<String> to String[]
  480. ArrayList<String> alCats = selectAllCats();
  481. return alCats.toArray(new String[alCats.size()]);
  482. }//selectAllCats1
  483.  
  484. public ArrayList<String> selectAllCats(){
  485. ArrayList<String> alRet = new ArrayList<>();
  486.  
  487. SQLiteDatabase db = this.getReadableDatabase();
  488. if (db!=null){
  489. Cursor cursor =
  490. db.rawQuery(
  491. "SELECT * FROM "+TABLE_CATEGORIES,
  492. null
  493. );
  494.  
  495. cursor.moveToFirst();
  496. while(!cursor.isAfterLast()){
  497. String strCatName = cursor.getString(
  498. cursor.getColumnIndex(COL_NAME)
  499. );
  500. alRet.add(strCatName);
  501. cursor.moveToNext();
  502. }//while
  503. db.close();
  504. }
  505. return alRet;
  506. }
  507. }//CategoriesDB
  508.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement