1
1
package com .incomingcall ;
2
2
3
3
import android .app .KeyguardManager ;
4
- import android .content .Intent ;
4
+ import android .media .AudioManager ;
5
+ import android .media .Ringtone ;
6
+ import android .media .RingtoneManager ;
5
7
import android .os .Build ;
6
8
import android .os .Bundle ;
9
+ import android .os .VibrationEffect ;
7
10
import android .util .Log ;
8
11
import android .view .WindowManager ;
9
12
import android .widget .ImageView ;
10
13
import android .widget .TextView ;
11
14
import android .view .View ;
12
- import android .net .Uri ;
13
15
import android .os .Vibrator ;
14
16
import android .content .Context ;
15
17
import android .media .MediaPlayer ;
16
18
import android .provider .Settings ;
17
19
import java .util .List ;
18
20
import java .util .Timer ;
19
21
import java .util .TimerTask ;
20
-
21
22
import android .app .Activity ;
22
-
23
23
import androidx .appcompat .app .AppCompatActivity ;
24
- import android .app .ActivityManager ;
25
- import android .app .ActivityManager .RunningAppProcessInfo ;
26
24
27
25
import com .facebook .react .bridge .Arguments ;
28
- import com .facebook .react .bridge .ReactContext ;
29
26
import com .facebook .react .bridge .ReadableMap ;
30
27
import com .facebook .react .bridge .WritableMap ;
31
- import com .facebook .react .bridge .ReactApplicationContext ;
32
28
import com .facebook .react .modules .core .DeviceEventManagerModule ;
33
29
34
30
import com .squareup .picasso .Picasso ;
@@ -42,19 +38,24 @@ public class UnlockScreenActivity extends AppCompatActivity implements UnlockScr
42
38
private Integer timeout = 0 ;
43
39
private String uuid = "" ;
44
40
static boolean active = false ;
45
- private static Vibrator v = (Vibrator ) IncomingCallModule .reactContext .getSystemService (Context .VIBRATOR_SERVICE );
46
- private long [] pattern = {0 , 1000 , 800 };
47
- private static MediaPlayer player = MediaPlayer .create (IncomingCallModule .reactContext , Settings .System .DEFAULT_RINGTONE_URI );
41
+ private static Vibrator vibrator ;
42
+ private static Ringtone ringtone ;
48
43
private static Activity fa ;
49
44
private Timer timer ;
45
+ static UnlockScreenActivity instance ;
46
+
47
+
48
+ public static UnlockScreenActivity getInstance () {
49
+ return instance ;
50
+ }
50
51
51
52
52
53
@ Override
53
54
public void onStart () {
54
55
super .onStart ();
55
56
if (this .timeout > 0 ) {
56
- this . timer = new Timer ();
57
- this . timer .schedule (new TimerTask () {
57
+ timer = new Timer ();
58
+ timer .schedule (new TimerTask () {
58
59
@ Override
59
60
public void run () {
60
61
// this code will be executed after timeout seconds
@@ -63,6 +64,7 @@ public void run() {
63
64
}, timeout );
64
65
}
65
66
active = true ;
67
+ instance = this ;
66
68
}
67
69
68
70
@ Override
@@ -76,7 +78,6 @@ protected void onCreate(Bundle savedInstanceState) {
76
78
super .onCreate (savedInstanceState );
77
79
78
80
fa = this ;
79
-
80
81
setContentView (R .layout .activity_call_incoming );
81
82
82
83
tvName = findViewById (R .id .tvName );
@@ -111,17 +112,16 @@ protected void onCreate(Bundle savedInstanceState) {
111
112
getWindow ().addFlags (WindowManager .LayoutParams .FLAG_FULLSCREEN | WindowManager .LayoutParams .FLAG_TURN_SCREEN_ON | WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON
112
113
| WindowManager .LayoutParams .FLAG_SHOW_WHEN_LOCKED | WindowManager .LayoutParams .FLAG_DISMISS_KEYGUARD );
113
114
114
- v . vibrate ( pattern , 0 );
115
- player . start ();
115
+ ringPhone ( );
116
+
116
117
117
118
AnimateImage acceptCallBtn = findViewById (R .id .ivAcceptCall );
118
119
acceptCallBtn .setOnClickListener (new View .OnClickListener () {
119
120
@ Override
120
121
public void onClick (View view ) {
121
122
try {
122
- v .cancel ();
123
- player .stop ();
124
- player .prepareAsync ();
123
+ vibrator .cancel ();
124
+ ringtone .stop ();
125
125
acceptDialing ();
126
126
} catch (Exception e ) {
127
127
WritableMap params = Arguments .createMap ();
@@ -136,9 +136,7 @@ public void onClick(View view) {
136
136
rejectCallBtn .setOnClickListener (new View .OnClickListener () {
137
137
@ Override
138
138
public void onClick (View view ) {
139
- v .cancel ();
140
- player .stop ();
141
- player .prepareAsync ();
139
+ stopRinging ();
142
140
dismissDialing ();
143
141
}
144
142
});
@@ -157,7 +155,34 @@ public void dismissIncoming() {
157
155
dismissDialing ();
158
156
}
159
157
160
- private void acceptDialing () {
158
+ private void ringPhone (){
159
+ long [] pattern = {0 , 1000 , 800 };
160
+ vibrator = (Vibrator ) getSystemService (Context .VIBRATOR_SERVICE );
161
+ int ringerMode = ((AudioManager ) getSystemService (Context .AUDIO_SERVICE )).getRingerMode ();
162
+ if (ringerMode == AudioManager .RINGER_MODE_SILENT ) return ;
163
+
164
+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
165
+ VibrationEffect vibe = VibrationEffect .createWaveform (pattern , 2 );
166
+ vibrator .vibrate (vibe );
167
+ }else {
168
+ vibrator .vibrate (pattern , 0 );
169
+ }
170
+ if (ringerMode == AudioManager .RINGER_MODE_VIBRATE ) return ;
171
+
172
+ ringtone = RingtoneManager .getRingtone (this , RingtoneManager .getActualDefaultRingtoneUri (getApplicationContext (), RingtoneManager .TYPE_RINGTONE ));
173
+ ringtone .play ();
174
+ }
175
+
176
+ private void stopRinging () {
177
+ if (vibrator != null ){
178
+ vibrator .cancel ();
179
+ }
180
+ int ringerMode = ((AudioManager ) getSystemService (Context .AUDIO_SERVICE )).getRingerMode ();
181
+ if (ringerMode != AudioManager .RINGER_MODE_NORMAL ) return ;
182
+ ringtone .stop ();
183
+ }
184
+
185
+ private void acceptDialing () {
161
186
WritableMap params = Arguments .createMap ();
162
187
params .putBoolean ("accept" , true );
163
188
params .putString ("uuid" , uuid );
0 commit comments