Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom ringtone #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ useEffect(() => {
}, []);
```

In `index.js` or anywhere firebase background handler lies:
In `index.js` or anywhere firebase background handler lies:

```javascript
import messaging from '@react-native-firebase/messaging';
Expand All @@ -95,13 +95,13 @@ messaging().setBackgroundMessageHandler(async remoteMessage => {
// Receive remote message
if (remoteMessage?.notification?.title === 'Incoming Call') {
// Display incoming call activity.
IncomingCall.display(
'callUUIDv4', // Call UUID v4
'Quocs', // Username
'https://avatars3.githubusercontent.com/u/16166195', // Avatar URL
'Incomming Call', // Info text
20000 // Timeout for end call after 20s
);
IncomingCall.display({
uuid: 'callUUIDv4', // Call UUID v4
name: 'Quocs', // Username
avatar: 'https://avatars3.githubusercontent.com/u/16166195', // Avatar URL
info: 'Incomming Call', // Info text
timeout: 20000 // Timeout for end call after 20s
});
} else if (remoteMessage?.notification?.title === 'Missed Call') {
// Terminate incoming activity. Should be called when call expired.
IncomingCall.dismiss();
Expand Down
28 changes: 20 additions & 8 deletions android/src/main/java/com/incomingcall/IncomingCallModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;

Expand All @@ -34,27 +35,38 @@ public String getName() {
}

@ReactMethod
public void display(String uuid, String name, String avatar, String info, int timeout) {
public void display(ReadableMap options) {
if (UnlockScreenActivity.active) {
return;
}
if (reactContext != null) {
Bundle bundle = new Bundle();
bundle.putString("uuid", uuid);
bundle.putString("name", name);
bundle.putString("avatar", avatar);
bundle.putString("info", info);
bundle.putInt("timeout", timeout);
bundle.putString("uuid", options.getString("uuid"));
bundle.putString("name", options.getString("name"));
bundle.putString("avatar", options.getString("avatar"));
bundle.putString("info", options.getString("info"));
bundle.putString("ringtone", options.getString("ringtone"));
bundle.putString("font", options.getString("font"));
bundle.putInt("timeout", options.getInt("timeout"));
Intent i = new Intent(reactContext, UnlockScreenActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED +
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD +
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

i.putExtras(bundle);
reactContext.startActivity(i);


}
}

@ReactMethod
public void dismiss() {

if (UnlockScreenActivity.active) {
UnlockScreenActivity.getInstance().dismissIncoming();
}
return;
}

private Context getAppContext() {
Expand Down
100 changes: 72 additions & 28 deletions android/src/main/java/com/incomingcall/UnlockScreenActivity.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
package com.incomingcall;

import android.app.KeyguardManager;
import android.content.Intent;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import android.net.Uri;
import android.os.Vibrator;
import android.content.Context;
import android.media.MediaPlayer;
import android.provider.Settings;
import android.graphics.Typeface;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;

import androidx.appcompat.app.AppCompatActivity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import com.squareup.picasso.Picasso;
Expand All @@ -41,20 +38,26 @@ public class UnlockScreenActivity extends AppCompatActivity implements UnlockScr
private ImageView ivAvatar;
private Integer timeout = 0;
private String uuid = "";
private String ringtoneSound;
static boolean active = false;
private static Vibrator v = (Vibrator) IncomingCallModule.reactContext.getSystemService(Context.VIBRATOR_SERVICE);
private long[] pattern = {0, 1000, 800};
private static MediaPlayer player = MediaPlayer.create(IncomingCallModule.reactContext, Settings.System.DEFAULT_RINGTONE_URI);
private static Vibrator vibrator;
private static Ringtone ringtone;
private static Activity fa;
private Timer timer;
static UnlockScreenActivity instance;


public static UnlockScreenActivity getInstance() {
return instance;
}


@Override
public void onStart() {
super.onStart();
if (this.timeout > 0) {
this.timer = new Timer();
this.timer.schedule(new TimerTask() {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// this code will be executed after timeout seconds
Expand All @@ -63,6 +66,7 @@ public void run() {
}, timeout);
}
active = true;
instance = this;
}

@Override
Expand All @@ -76,7 +80,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

fa = this;

setContentView(R.layout.activity_call_incoming);

tvName = findViewById(R.id.tvName);
Expand All @@ -85,18 +88,28 @@ protected void onCreate(Bundle savedInstanceState) {

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.containsKey("uuid")) {
if (bundle.getString("uuid") != null) {
uuid = bundle.getString("uuid");
}
if (bundle.containsKey("name")) {
if (bundle.getString("name") != null) {
String name = bundle.getString("name");
tvName.setText(name);
}
if (bundle.containsKey("info")) {
if (bundle.getString("info") != null) {
String info = bundle.getString("info");
tvInfo.setText(info);
}
if (bundle.containsKey("avatar")) {
if (bundle.getString("font") != null) {
String font = bundle.getString("font");
Typeface tf = Typeface.createFromAsset(getAssets(), font);
tvName.setTypeface(tf);
tvInfo.setTypeface(tf);
}
if (bundle.getString("ringtone") != null) {
String filename = bundle.getString("ringtone");
ringtoneSound = filename.contains(".") ? filename.substring(0, filename.lastIndexOf('.')) : filename;
}
if (bundle.getString("avatar") != null) {
String avatar = bundle.getString("avatar");
if (avatar != null) {
Picasso.get().load(avatar).transform(new CircleTransform()).into(ivAvatar);
Expand All @@ -105,23 +118,21 @@ protected void onCreate(Bundle savedInstanceState) {
if (bundle.containsKey("timeout")) {
this.timeout = bundle.getInt("timeout");
}
else this.timeout = 0;
}

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

v.vibrate(pattern, 0);
player.start();
ringPhone();


AnimateImage acceptCallBtn = findViewById(R.id.ivAcceptCall);
acceptCallBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
v.cancel();
player.stop();
player.prepareAsync();
vibrator.cancel();
ringtone.stop();
acceptDialing();
} catch (Exception e) {
WritableMap params = Arguments.createMap();
Expand All @@ -136,9 +147,7 @@ public void onClick(View view) {
rejectCallBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
v.cancel();
player.stop();
player.prepareAsync();
stopRinging();
dismissDialing();
}
});
Expand All @@ -157,7 +166,42 @@ public void dismissIncoming() {
dismissDialing();
}

private void acceptDialing() {
private void ringPhone(){
long[] pattern = {0, 1000, 800};
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
int ringerMode = ((AudioManager) getSystemService(Context.AUDIO_SERVICE)).getRingerMode();
if(ringerMode == AudioManager.RINGER_MODE_SILENT) return;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
VibrationEffect vibe = VibrationEffect.createWaveform(pattern, 2);
vibrator.vibrate(vibe);
}else{
vibrator.vibrate(pattern, 0);
}
if(ringerMode == AudioManager.RINGER_MODE_VIBRATE) return;

if (ringtoneSound != null) {
Uri ringtoneUri = Uri.parse("android.resource://" + getPackageName() + "/raw/" + ringtoneSound);
ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ringtone.setLooping(true);
}
} else {
ringtone = RingtoneManager.getRingtone(this, RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE));
}
ringtone.play();
}

private void stopRinging() {
if (vibrator != null){
vibrator.cancel();
}
int ringerMode = ((AudioManager) getSystemService(Context.AUDIO_SERVICE)).getRingerMode();
if(ringerMode != AudioManager.RINGER_MODE_NORMAL) return;
ringtone.stop();
}

private void acceptDialing() {
WritableMap params = Arguments.createMap();
params.putBoolean("accept", true);
params.putString("uuid", uuid);
Expand Down
21 changes: 7 additions & 14 deletions android/src/main/res/layout/activity_call_incoming.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
>

<RelativeLayout
android:layout_width="170dp"
android:layout_height="170dp"
style="@style/avatarWrapper"
>
<ImageView
android:id="@+id/ivAvatar"
Expand Down Expand Up @@ -58,19 +57,15 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="22sp"
android:fontFamily="sans-serif"
android:textColor="#FFFFFF"
style="@style/tvName"
/>

<TextView
android:id="@+id/tvInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:fontFamily="sans-serif"
android:textColor="#ADADAD"
style="@style/tvInfo"
/>

</LinearLayout>
Expand All @@ -81,7 +76,7 @@
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
style="@style/buttonWrapper"
>

<LinearLayout
Expand All @@ -95,10 +90,9 @@
>
<com.incomingcall.AnimateImage
android:id="@+id/ivDeclineCall"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="centerInside"
android:src="@drawable/ic_decline_call"
style="@style/ivDeclineCall"
/>
<TextView
android:id="@+id/tvDecline"
Expand All @@ -123,10 +117,9 @@
>
<com.incomingcall.AnimateImage
android:id="@+id/ivAcceptCall"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="centerInside"
android:src="@drawable/ic_accept_call"
style="@style/ivAcceptCall"
/>
<TextView
android:id="@+id/tvAccept"
Expand All @@ -142,4 +135,4 @@
</LinearLayout>

</LinearLayout>
</RelativeLayout>
</RelativeLayout>
28 changes: 28 additions & 0 deletions android/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="avatarWrapper">
<item name="android:layout_width">170dp</item>
<item name="android:layout_height">170dp</item>
</style>
<style name="buttonWrapper">
<item name="android:layout_marginBottom">60dp</item>
</style>
<style name="tvName">
<item name="android:textSize">22sp</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="tvInfo">
<item name="android:textSize">14sp</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textColor">#ADADAD</item>
</style>
<style name="ivAcceptCall">
<item name="android:layout_width">60sp</item>
<item name="android:layout_height">60sp</item>
</style>
<style name="ivDeclineCall">
<item name="android:layout_width">60sp</item>
<item name="android:layout_height">60sp</item>
</style>
</resources>
Loading