Advertisement
eranseg

Broadcast Receiver Exercise

Nov 23rd, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.85 KB | None | 0 0
  1. //--------------------- AndroidManifest.xml -----------------------
  2.  
  3. <?xml version="1.0" encoding="utf-8"?>
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.     xmlns:tools="http://schemas.android.com/tools"
  6.     package="com.sgl.broadcastreceiver">
  7.  
  8.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  9.     <uses-permission android:name="android.permission.READ_CALL_LOG" />
  10.  
  11.     <application
  12.         android:allowBackup="true"
  13.         android:icon="@mipmap/ic_launcher"
  14.         android:label="@string/app_name"
  15.         android:roundIcon="@mipmap/ic_launcher_round"
  16.         android:supportsRtl="true"
  17.         android:theme="@style/AppTheme"
  18.         tools:ignore="GoogleAppIndexingWarning">
  19.         <receiver
  20.             android:name=".CallReceiver"
  21.             android:enabled="true"
  22.             android:exported="true">
  23.             <intent-filter>
  24.                 <action android:name="android.intent.action.PHONE_STATE"/>
  25.                 <action android:name="android.intent.action.CALL_LOG"/>
  26.             </intent-filter>
  27.         </receiver>
  28.  
  29.         <activity android:name=".MainActivity">
  30.             <intent-filter>
  31.                 <action android:name="android.intent.action.MAIN" />
  32.  
  33.                 <category android:name="android.intent.category.LAUNCHER" />
  34.             </intent-filter>
  35.         </activity>
  36.     </application>
  37. </manifest>
  38.  
  39.  
  40. // --------------------------- MainActivity.java ----------------------------
  41.  
  42. package com.sgl.broadcastreceiver;
  43.  
  44. import androidx.appcompat.app.AppCompatActivity;
  45. import androidx.core.app.ActivityCompat;
  46. import androidx.core.content.ContextCompat;
  47.  
  48. import android.Manifest;
  49. import android.app.Activity;
  50. import android.content.Context;
  51. import android.content.IntentFilter;
  52. import android.content.SharedPreferences;
  53. import android.content.pm.PackageManager;
  54. import android.os.Bundle;
  55. import android.telephony.TelephonyManager;
  56. import android.view.View;
  57. import android.widget.AdapterView;
  58. import android.widget.ListView;
  59. import android.widget.TextView;
  60.  
  61. import java.util.ArrayList;
  62. import java.util.Set;
  63.  
  64. public class MainActivity extends AppCompatActivity {
  65.  
  66.     Context context;
  67.     private ArrayList<String> callsList;
  68.     private static final String SHARED_PREFERENCES = "sharedPreferences";
  69.     private static int index = 0;
  70.     private ListView incomingList;
  71.     SharedPreferences sharedPreferences;
  72.     SharedPreferences.Editor editor;
  73.  
  74.     @Override
  75.     protected void onCreate(Bundle savedInstanceState) {
  76.         super.onCreate(savedInstanceState);
  77.         setContentView(R.layout.activity_main);
  78.         setPointer();
  79.     }
  80.  
  81.     private void setPointer() {
  82.         this.context = this;
  83.         sharedPreferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
  84.         editor = sharedPreferences.edit();
  85.         callsList = new ArrayList<>();
  86.         if((ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) &&
  87.            (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED)) {
  88.                 ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
  89.                 ActivityCompat.requestPermissions((Activity) context, new String[] {Manifest.permission.READ_CALL_LOG}, 1);
  90.         } else {
  91.             // Permission is already granted
  92.             incomingList = findViewById(R.id.incoming_calls_list);
  93.             if(index == 0) clearSPList();
  94.             registerReceiver(new CallReceiver(), new IntentFilter(TelephonyManager.EXTRA_INCOMING_NUMBER));
  95.             Bundle extras = getIntent().getExtras();
  96.             if(extras != null) {
  97.                 String phNumber = extras.getString("phoneNumber");
  98.                 saveData(phNumber);
  99.                 loadData();
  100.             }
  101.             incomingList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  102.                 @Override
  103.                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  104.                     if(sharedPreferences.getAll().size() > 0) {
  105.                         editor.remove(((TextView)view.findViewById(R.id.callNumber)).getText().toString());
  106.                         editor.commit();
  107.                         index--;
  108.                         loadData();
  109.                     }
  110.                 }
  111.             });
  112.         }
  113.     }
  114.  
  115.     private void saveData(String data) {
  116.         int x = Integer.parseInt(sharedPreferences.getString(data, "0"));
  117.         x += 1;
  118.         editor.putString(data, Integer.toString(x));
  119.         index++;
  120.         editor.commit();
  121.     }
  122.  
  123.     private void clearSPList() {
  124.         editor.clear();
  125.         editor.commit();
  126.         callsList.clear();
  127.     }
  128.  
  129.     private void loadData() {
  130.         callsList.clear();
  131.         callsList.addAll(sharedPreferences.getAll().keySet());
  132.         for(int i = 0; i < callsList.size(); i++) {
  133.             if(Integer.parseInt(sharedPreferences.getString(callsList.get(i), "-1")) > 1) {
  134.                 callsList.set(i, callsList.get(i) + " (" + sharedPreferences.getString(callsList.get(i), "-1") + ")");
  135.             }
  136.         }
  137.         CallAdapter callAdapter = new CallAdapter(context, callsList);
  138.         incomingList.setAdapter(callAdapter);
  139.     }
  140. }
  141.  
  142.  
  143. // ---------------------- CallReceiver.java -----------------------------
  144.  
  145. package com.sgl.broadcastreceiver;
  146.  
  147. import android.content.BroadcastReceiver;
  148. import android.content.Context;
  149. import android.content.Intent;
  150. import android.telephony.TelephonyManager;
  151.  
  152. public class CallReceiver extends BroadcastReceiver {
  153.  
  154.     @Override
  155.     public void onReceive(Context context, Intent intent) {
  156.         if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
  157.             String phNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
  158.             if(phNumber == null) return;
  159.             Intent intent1 = new Intent(context, MainActivity.class);
  160.             intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  161.             intent1.putExtra("phoneNumber", phNumber);
  162.             context.startActivity(intent1);
  163.         }
  164.     }
  165. }
  166.  
  167.  
  168. //------------------------ CallAdapter.java -----------------------------
  169.  
  170. package com.sgl.broadcastreceiver;
  171.  
  172. import android.content.Context;
  173. import android.view.LayoutInflater;
  174. import android.view.View;
  175. import android.view.ViewGroup;
  176. import android.widget.BaseAdapter;
  177. import android.widget.TextView;
  178.  
  179. import java.util.ArrayList;
  180.  
  181. class CallAdapter extends BaseAdapter {
  182.  
  183.     Context context;
  184.     ArrayList<String> callsList;
  185.  
  186.     public CallAdapter(Context context, ArrayList<String> callsList) {
  187.         this.context = context;
  188.         this.callsList = callsList;
  189.     }
  190.  
  191.     @Override
  192.     public int getCount() {
  193.         return callsList.size();
  194.     }
  195.  
  196.     @Override
  197.     public Object getItem(int position) {
  198.         return callsList.get(position);
  199.     }
  200.  
  201.     @Override
  202.     public long getItemId(int position) {
  203.         return position;
  204.     }
  205.  
  206.     @Override
  207.     public View getView(int position, View convertView, ViewGroup parent) {
  208.         final View singleCall = LayoutInflater.from(context).inflate(R.layout.call_layout, null);
  209.         final TextView number = singleCall.findViewById(R.id.callNumber);
  210.         number.setText(callsList.get(position));
  211.         return singleCall;
  212.     }
  213. }
  214.  
  215.  
  216. // ------------------------ activity_main.xml ------------------------------------
  217.  
  218. <?xml version="1.0" encoding="utf-8"?>
  219. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  220.     xmlns:app="http://schemas.android.com/apk/res-auto"
  221.     xmlns:tools="http://schemas.android.com/tools"
  222.     android:layout_width="match_parent"
  223.     android:layout_height="match_parent"
  224.     tools:context=".MainActivity">
  225.  
  226.     <LinearLayout
  227.         android:layout_width="match_parent"
  228.         android:layout_height="match_parent"
  229.         android:orientation="vertical">
  230.  
  231.         <TextView
  232.             android:id="@+id/title"
  233.             android:layout_width="match_parent"
  234.             android:layout_height="match_parent"
  235.             android:layout_weight="6"
  236.             android:background="#3949AB"
  237.             android:gravity="center"
  238.             android:text="@string/call_receiver"
  239.             android:textColor="#FAFFFFFF"
  240.             android:textSize="36sp" />
  241.  
  242.         <ListView
  243.             android:id="@+id/incoming_calls_list"
  244.             android:layout_width="match_parent"
  245.             android:layout_height="match_parent"
  246.             android:layout_weight="1" />
  247.     </LinearLayout>
  248. </androidx.constraintlayout.widget.ConstraintLayout>
  249.  
  250.  
  251. // ---------------------- call_layout.xml -----------------------------
  252.  
  253. <?xml version="1.0" encoding="utf-8"?>
  254. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  255.     xmlns:app="http://schemas.android.com/apk/res-auto"
  256.     android:id="@+id/call_layout"
  257.     android:layout_width="match_parent"
  258.     android:layout_height="wrap_content"
  259.     android:layout_margin="20dp"
  260.     android:background="#3C00ACC1"
  261.     android:orientation="horizontal">
  262.  
  263.     <ImageView
  264.         android:id="@+id/imgCall"
  265.         android:layout_width="wrap_content"
  266.         android:layout_height="wrap_content"
  267.         android:layout_gravity="center_vertical"
  268.         android:layout_weight="1"
  269.         android:contentDescription="@string/img_call"
  270.         android:padding="5dp"
  271.         app:srcCompat="@drawable/call" />
  272.  
  273.     <TextView
  274.         android:id="@+id/callNumber"
  275.         android:layout_width="wrap_content"
  276.         android:layout_height="match_parent"
  277.         android:layout_weight="3"
  278.         android:gravity="center"
  279.         android:text="@string/_5555555555"
  280.         android:textSize="30sp" />
  281. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement