Skip to content

Instantly share code, notes, and snippets.

@sukso96100
Created August 30, 2018 12:02
Show Gist options
  • Save sukso96100/fac9345ec285454009076a12c24fe3c5 to your computer and use it in GitHub Desktop.
Save sukso96100/fac9345ec285454009076a12c24fe3c5 to your computer and use it in GitHub Desktop.
package com.youngbin.assistscreen.cardservices.missedcallcard;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.youngbin.assistscreen.R;
import com.youngbin.assistscreen.util.AddCardEvent;
import com.youngbin.assistscreen.util.BusProvider;
import com.youngbin.assistscreen.util.ContactUtil;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
public class MissedCallCardPusher extends Service {
private BroadcastReceiver BR;
private IntentFilter IF;
boolean ring=false;
boolean callReceived=false;
boolean pushed=false;
String callerPhoneNumber = null;
String name = null;
private String DEBUGTAG = "MissedCallCardPusher";
public MissedCallCardPusher() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
IF = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
BR = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(DEBUGTAG, "Phone State Changed");
// Get the current Phone State
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Log.i(DEBUGTAG, "Phone State:"+state);
if (state == null)
return;
// If phone state "Rininging"
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
ring = true;
// Get the Caller's Phone Number
Bundle bundle = intent.getExtras();
callerPhoneNumber = bundle.getString("incoming_number");
Log.i(DEBUGTAG, "Phone Ringing");
Log.i(DEBUGTAG,"Caller Number:"+callerPhoneNumber);
pushed=false;
}
// If incoming call is received
else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Log.i(DEBUGTAG, "Call Received");
callReceived = true;
pushed=false;
}
// If phone is Idle
else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
// If phone was ringing(ring=true) and not received(callReceived=false) , then it is a missed call
if (ring == true && callReceived == false && pushed == false) {
pushed=true;
Log.i(DEBUGTAG, "Registering BusProvider");
try{
BusProvider.getInstance().register(this);
}catch (Exception e){}
Log.i(DEBUGTAG,"Missed Call!");
Log.i(DEBUGTAG,"Caller Number:"+callerPhoneNumber);
//Get COntact Name and Image using Phone Number
ContactUtil CU = new ContactUtil(context, callerPhoneNumber);
LayoutInflater LI = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout RL = (RelativeLayout) LI.inflate(R.layout.card_missedcall, null);
TextView Name = (TextView) RL.findViewById(R.id.name);
TextView PhoneNumber = (TextView) RL.findViewById(R.id.phone);
TextView Time = (TextView) RL.findViewById(R.id.time);
ImageView Profile = (ImageView) RL.findViewById(R.id.profile);
Button CallNow = (Button) RL.findViewById(R.id.call);
if(CU.getContactName()==null){
name=getString(R.string.unknown_person);
}else{
name = CU.getContactName();
}
Name.setText(name);
PhoneNumber.setText(callerPhoneNumber);
Calendar Cal = Calendar.getInstance();
int year = Cal.get(Calendar.YEAR);
int month = Cal.get(Calendar.MONTH);
int day = Cal.get(Calendar.DAY_OF_MONTH);
int hour = Cal.get(Calendar.HOUR_OF_DAY);
int minute = Cal.get(Calendar.MINUTE);
Time.setText(year+"."+month+"."+day+" "+hour+":"+minute);
Bitmap profilebitmap = null;
try {
profilebitmap = CU.getContactImage();
} catch (Exception e) {
e.printStackTrace();
}
if(profilebitmap==null){
Profile.setImageDrawable(getResources().getDrawable(R.drawable.contact));}
else{Profile.setImageBitmap(profilebitmap);}
final String finalCallerPhoneNumber = callerPhoneNumber;
CallNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String calluri = "tel:" + finalCallerPhoneNumber;
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(calluri));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
Log.i(DEBUGTAG,"Sending Card");
BusProvider.getInstance().post
(new AddCardEvent(getString(R.string.missed_call),RL,"missedcall"));
try{
BusProvider.getInstance().unregister(this);
}catch (Exception e){}
CU.close();
}
}
}
};
registerReceiver(BR, IF);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(BR);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment