Skip to content

Instantly share code, notes, and snippets.

@lokcito
Last active February 1, 2020 18:09
Show Gist options
  • Save lokcito/836cc7dc2dca074a4e112ac9f4773b2a to your computer and use it in GitHub Desktop.
Save lokcito/836cc7dc2dca074a4e112ac9f4773b2a to your computer and use it in GitHub Desktop.
Pusher
implementation 'com.pusher:pusher-java-client:1.5.0'
public class DatabaseHelper extends SQLiteOpenHelper {
....
public static class Columns implements BaseColumns {
public static final String COLUMN_NAME_TOKEN = "token";
public static final String COLUMN_NAME_NAME = "name";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_MESSAGE = "message";
public static final String COLUMN_NAME_EXTRA = "extra";
public static final String COLUMN_NAME_ACTIVE = "is_active";
public static final String COLUMN_NAME_CHANNEL = "channel";
public static final String COLUMN_NAME_EVENT = "event";
public static final String COLUMN_NAME_CLIENT = "client";
}
...
public static final String NOTIFY_TABLE_NAME = "avispa_notifications";
private static final String NOTIFY_SQL_CREATE_TABLE =
"CREATE TABLE " + NOTIFY_TABLE_NAME + " (" +
Columns._ID + " INTEGER PRIMARY KEY," +
Columns.COLUMN_NAME_TITLE + " TEXT," +
Columns.COLUMN_NAME_MESSAGE + " TEXT," +
Columns.COLUMN_NAME_EXTRA + " TEXT," +
Columns.COLUMN_NAME_EVENT + " TEXT," +
Columns.COLUMN_NAME_CHANNEL + " TEXT," +
Columns.COLUMN_NAME_TOKEN + " TEXT)";
private static final String NOTIFY_SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + NOTIFY_TABLE_NAME;
...
@Override
public void onCreate(SQLiteDatabase db) {
...
db.execSQL(NOTIFY_SQL_CREATE_TABLE);
...
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
....
db.execSQL(NOTIFY_SQL_DELETE_ENTRIES);
...
onCreate(db);
}
<vector android:height="24dp" android:viewportHeight="80"
android:viewportWidth="87" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillAlpha="1" android:fillColor="#489034"
android:fillType="nonZero"
android:pathData="m67.1988,57.547c0.7264,0.1235 1.4626,0.2797 2.2355,0.4874 3.0561,0.821 6.0851,1.7519 9.1454,2.5609 2.1604,0.5708 5.4691,0.673 6.9769,2.523 1.1959,1.4707 0.635,3.4509 -0.6491,4.6499 -2.069,1.9367 -5.1463,2.3139 -7.832,2.7058 -3.5405,0.5165 -7.1088,0.8312 -10.5286,1.9153 -5.7319,1.8167 -10.9753,4.4538 -16.9937,5.2309 -2.9937,0.3867 -6.0254,0.1482 -9.0272,0.4064 -2.8148,0.2421 -5.5322,0.5638 -8.3471,0.1073 -2.6105,-0.4233 -5.2049,-0.8086 -7.868,-0.6841 -2.4289,0.114 -4.6066,1.0999 -6.9723,1.3741 -3.0801,0.3566 -6.7546,-3.8925 -7.317,-6.6788 -0.9941,-4.9255 5.5986,-5.1784 8.5132,-7.4158 1.0428,-0.8008 1.8373,-1.8598 2.8406,-2.6924 1.0845,-0.9005 2.3209,-1.5893 3.3549,-2.5597 1.0629,-0.9984 2.3809,-1.8679 3.743,-2.4097 3.6495,-1.4525 7.45,-2.4959 11.2617,-3.4242 5.1502,-1.2542 12.3966,-2.7118 16.8511,1.2157 1.7643,1.5557 3.5712,1.9912 5.9595,2.183 1.6725,0.1345 3.1422,0.248 4.6532,0.5049"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#337431"
android:fillType="nonZero"
android:pathData="m55.3603,57.5624c2.2892,-0.1393 4.5018,0.9814 6.7592,1.1056 2.1368,0.1176 4.3339,0.8442 6.2731,1.7234 0.9303,0.4216 3.9931,1.1197 4.2855,2.1805 0.5556,2.0165 -7.3843,3.8711 -8.7637,4.2429 -2.3174,0.6248 -4.2951,1.7713 -6.5377,2.5347 -2.722,0.9263 -5.5005,1.6023 -8.1964,2.6597 -3.4925,1.3691 -7.2499,2.6309 -10.9523,3.2857 -3.2505,0.5747 -6.1976,0.205 -9.4305,-0.2003 -3.3613,-0.4213 -9.4904,0.3047 -11.2603,-3.3895 -0.8654,-1.8059 0.3951,-2.0977 1.8496,-2.7665 2.3809,-1.095 4.5925,-2.4919 6.9377,-3.6618 6.4544,-3.2198 13.7072,-5.0999 20.7701,-6.3292 2.6839,-0.4674 5.552,-1.2203 8.2656,-1.3851"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#e4e4e3"
android:fillType="nonZero"
android:pathData="m52.1232,67.4635c-6.0812,4.7559 -21.6817,8.104 -26.8764,0.0904 -2.462,-3.7998 -1.5677,-8.4569 -0.6113,-12.8433 0.3034,-1.3913 0.6113,-2.7535 0.8248,-4.0502 0.8244,-5.0553 1.2142,-10.5043 1.1405,-15.621 -0.0286,-2.0475 -0.4512,-3.9804 -0.3856,-5.6585 0.0409,-1.0873 0.2829,-2.068 0.9723,-2.9051 0.4268,-0.5168 1.0216,-0.9807 1.8426,-1.3829 0.3447,-0.1683 0.7345,-0.3239 1.1652,-0.4717 3.3648,-1.1409 6.2861,0.3158 8.9288,2.3756 2.9788,2.3227 5.2687,5.4123 7.3942,8.5965 1.7396,2.6137 3.3729,5.2892 5.1866,7.6733 3.0568,4.0209 5.4818,7.9604 6.1588,11.7968 0.7592,4.2715 -0.6442,8.416 -5.7404,12.4002"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#bebdbb"
android:fillType="nonZero"
android:pathData="m57.8636,55.0634c-0.0161,0 -0.0286,0.004 -0.0452,0.008 -1.2718,0.3324 -1.3663,4.2552 -1.953,5.158 -4.0047,6.1549 -16.0027,9.2653 -21.4193,3.3358 -3.1923,-3.4959 -3.5084,-8.8262 -3.6354,-13.7297v-0.004c2.0352,-5.4779 7.2051,-9.6961 12.1743,-12.392 1.0259,-0.5539 2.2938,-1.1448 3.5331,-1.8464 -2.1255,-3.1842 -4.4154,-6.2738 -7.3942,-8.5965 -2.6427,-2.0599 -5.564,-3.5165 -8.9288,-2.3756 -0.4307,0.1478 -0.8206,0.3034 -1.1652,0.4717 -0.4639,0.5091 -0.8904,1.0548 -1.2926,1.6334 -0.1849,-0.115 -0.3651,-0.1972 -0.55,-0.2504 -0.6893,0.8371 -0.9313,1.8178 -0.9723,2.9051 -0.0656,1.6782 0.357,3.611 0.3856,5.6586 0.0737,5.1167 -0.3161,10.5657 -1.1405,15.621 -0.2134,1.2967 -0.5214,2.6589 -0.8248,4.0502 -0.9564,4.3864 -1.8507,9.0435 0.6114,12.8433 5.1946,8.0136 20.7952,4.6655 26.8763,-0.0904 5.0963,-3.9841 6.4996,-8.1286 5.7404,-12.4001"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#c01e2b"
android:fillType="nonZero"
android:pathData="m63.6964,22.5701c-1.3388,4.0689 -7.1233,6.6894 -10.5117,8.5983 -7.7657,4.3759 -16.1449,8.4441 -24.5995,11.376 -7.2337,2.5054 -20.7127,8.5355 -25.8343,-1.0495 -3.1041,-5.8035 1.6143,-14.3707 7.4337,-21.3462 2.4187,-2.8967 5.0253,-5.5242 7.3448,-7.559 1.416,-1.2428 2.7206,-2.2673 3.81,-3.0032 4.9343,-3.3299 10.8955,-4.5307 16.8811,-4.3917 3.6477,0.0822 7.3078,0.6615 10.7477,1.5572 0.1753,0.0462 0.3588,0.0938 0.5408,0.1496 6.5268,1.8404 16.9499,7.2753 14.1873,15.6686"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#d52c49"
android:fillType="nonZero"
android:pathData="m46.4061,28.3814c-5.3894,1.3758 -12.1423,1.0703 -17.2156,-1.1829 -4.7332,-2.1022 -6.4509,-7.9386 -4.8747,-12.5942 1.8553,-5.4797 9.2499,-8.3446 14.5898,-8.8 5.9443,-0.5073 12.1994,1.2866 16.958,4.7498 3.5743,2.6014 6.525,6.3401 4.0206,10.9135 -2.4278,4.4327 -9.0311,5.7782 -13.4782,6.9138"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#e4e4e3"
android:fillType="nonZero"
android:pathData="m47.7537,11.4908c-2.1438,1.9565 -6.9039,2.128 -8.9528,-0.0106 -1.6249,-1.6926 -1.5752,-4.1603 -0.5803,-6.2854 3.6477,0.0822 7.3078,0.6615 10.7477,1.5572 0.1753,0.0462 0.3588,0.0938 0.5408,0.1496 0.0974,1.6933 -0.4466,3.3909 -1.7554,4.5893"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#ffffff"
android:fillType="nonZero"
android:pathData="m50.6831,23.7004c-3.4294,-3.5782 2.0158,-11.2695 6.664,-8.1379 2.6179,1.7642 3.2678,5.9545 0.8717,8.1456 -1.8037,1.6492 -5.8095,1.7932 -7.5357,-0.008"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#ffffff"
android:fillType="nonZero"
android:pathData="m30.4937,34.5734c-3.4293,-3.5779 2.0161,-11.2695 6.664,-8.1375 2.6183,1.7643 3.2681,5.9542 0.8717,8.1453 -1.8037,1.6496 -5.8092,1.7935 -7.5357,-0.008"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#ffffff"
android:fillType="nonZero"
android:pathData="m7.3342,35.3728c-3.4293,-3.5782 2.0158,-11.2695 6.664,-8.1379 2.6183,1.7642 3.2678,5.9542 0.8717,8.1456 -1.8037,1.6492 -5.8095,1.7932 -7.5357,-0.008"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#e4e4e3"
android:fillType="nonZero"
android:pathData="m17.4751,19.9246c-1.7216,1.5741 -5.4554,1.7773 -7.2905,0.224 2.4187,-2.8967 5.0253,-5.5242 7.3448,-7.559 1.8108,2.0193 2.0267,5.4303 -0.0543,7.3349"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#e4e4e3"
android:fillType="nonZero"
android:pathData="m64.2746,60.5289c-1.6658,2.4907 -4.2263,3.4058 -6.5528,-0.086 -0.9028,-1.3543 -0.8904,-3.5698 -0.8001,-5.1209 0.1559,-2.6098 1.4442,-8.0998 5.1823,-7.0824 1.3624,0.3694 2.3266,1.1448 2.9626,2.1544 1.7808,2.8064 1.0259,7.4147 -0.792,10.1349"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#bebdbb"
android:fillType="nonZero"
android:pathData="m64.2499,60.459c0.008,0.0249 0.0167,0.0495 0.0246,0.0699 -1.6658,2.4907 -4.2263,3.4058 -6.5528,-0.086 -0.9028,-1.3543 -0.8904,-3.5698 -0.8001,-5.1209 0.1559,-2.6098 1.4443,-8.0998 5.1823,-7.0824 1.3624,0.3694 2.3266,1.1448 2.9626,2.1544 -0.4882,0.5006 -1.3458,0.677 -1.9614,0.9436 -1.1818,0.5129 -2.0846,1.2062 -2.6137,2.417 -0.6361,1.4484 -0.242,4.4681 1.2926,5.4778 1.034,0.677 1.9325,-0.1767 2.4659,1.2265"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#c01e2b"
android:fillType="nonZero"
android:pathData="m59.2616,51.0629c-1.7088,0.005 -5.4903,0.2218 -4.9576,-2.0518 0.5257,-2.2443 3.2794,-6.0854 5.6413,-6.7356 2.2423,-0.617 4.4044,0.1316 6.0142,1.6404 1.2915,1.2111 3.7134,4.288 2.3417,6.1807 -1.5812,2.1827 -6.8012,0.9602 -9.0396,0.9663"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#d52c49"
android:fillType="nonZero"
android:pathData="m61.6602,43.4987c2.2592,-1.5469 7.2041,3.0713 5.3696,5.0662 -1.6863,1.8337 -8.4257,-2.9739 -5.3696,-5.0662"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#489034"
android:fillType="nonZero"
android:pathData="m73.2458,59.532c-0.2279,-0.9529 -0.6812,-1.7251 -1.3536,-2.5646 0.9969,-0.5631 2.1706,-0.003 2.6222,1.0494 0.3045,-0.9381 0.9754,-1.7501 2.0994,-1.7773 -0.2685,0.5074 -0.465,1.0506 -0.6452,1.6031 0.8283,-0.6019 1.905,-0.3164 2.8367,-0.2322 -0.5486,0.4283 -1.0604,0.8722 -1.5483,1.3466 0.9585,0.1511 2.086,-0.2099 2.6977,-1.0181 0.0127,0.744 0.0409,1.4446 -0.2974,2.0884 0.5143,-0.443 1.1285,-0.4547 1.6683,-0.8573 -0.0642,0.3806 0.0646,0.8023 0.0172,1.1684 0.2498,-0.2691 0.611,-0.4441 0.7895,-0.7553 1.5402,1.5645 0.7405,4.7329 -1.7741,4.4623l-7.1124,-4.5134"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
<path android:fillAlpha="1" android:fillColor="#489034"
android:fillType="nonZero"
android:pathData="m14.5685,67.465c-0.5232,-0.8368 -1.47,-1.1779 -2.4172,-1.1216 0.9606,-0.6498 1.6651,-0.604 2.8141,-0.6843 -0.3566,-0.8132 -0.4963,-1.6395 -0.2332,-2.557 0.3379,0.3779 0.6142,0.895 1.0407,1.2031 0.1372,-1.1332 1.0883,-2.0515 1.9177,-2.7076 -0.0706,0.6798 -0.3228,1.3648 -0.0819,2.0267 0.4632,-0.5391 1.1233,-1.4303 1.863,-1.6098 -0.3937,1.6087 0.4568,1.8817 1.252,3.2622l-6.1553,2.1883"
android:strokeColor="#00000000" android:strokeWidth="0.03527778"/>
</vector>
package info.rayrojas.avispa;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Credentials;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.hudomju.swipe.OnItemClickListener;
import com.hudomju.swipe.SwipeToDismissTouchListener;
import com.hudomju.swipe.adapter.ListViewAdapter;
import java.util.ArrayList;
import info.rayrojas.avispa.activities.CredentialsActivity;
import info.rayrojas.avispa.adapters.NotifyAdapter;
import info.rayrojas.avispa.conf.Settings;
import info.rayrojas.avispa.models.Channel;
import info.rayrojas.avispa.models.Credential;
import info.rayrojas.avispa.models.Event;
import info.rayrojas.avispa.models.Notify;
import info.rayrojas.avispa.notifiers.NotificationView;
import info.rayrojas.avispa.services.SpyService;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
ServiceConnection, SpyService.CallBack {
EditText editText;
ListView listViewNotifications;
ArrayList<Notify> items;
NotifyAdapter itemsAdapter;
SpyService serviceCommunitcation;
Boolean wasBinded = false;
Notify list;
Boolean myFirstTime = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateRows();
Snackbar.make(view, "Refrescando", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
list = new Notify();
listViewNotifications = (ListView) findViewById(R.id.listNotifications);
items = list.getAll(this);
itemsAdapter =
new NotifyAdapter(this, items);
listViewNotifications.setAdapter(itemsAdapter);
final SwipeToDismissTouchListener<ListViewAdapter> touchListener =
new SwipeToDismissTouchListener<>(
new ListViewAdapter(listViewNotifications),
new SwipeToDismissTouchListener.DismissCallbacks<ListViewAdapter>() {
@Override
public boolean canDismiss(int position) {
return true;
}
@Override
public void onDismiss(ListViewAdapter view, int position) {
itemsAdapter.remove(position);
updateRows();
}
});
listViewNotifications.setOnTouchListener(touchListener);
listViewNotifications.setOnScrollListener((AbsListView.OnScrollListener) touchListener.makeScrollListener());
listViewNotifications.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (touchListener.existPendingDismisses()) {
touchListener.undoPendingDismiss();
} else {
Intent currentIntent = new Intent(MainActivity.this, NotificationView.class);
Notify currentNotify = itemsAdapter.getOneByPosition(position);
if ( currentNotify == null ) {
Toast.makeText(MainActivity.this, "Error en la selección.", Toast.LENGTH_SHORT).show();
return;
}
currentIntent.putExtra("notifyId", currentNotify.getId());
startActivity(currentIntent);
}
}
});
}
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// if (touchListener.existPendingDismisses()) {
// touchListener.undoPendingDismiss();
// } else {
// Toast.makeText(MainActivity.this, "Position " + position, Toast.LENGTH_SHORT).show();
// }
// }
public void doStartService() {
Intent mServiceIntent = new Intent(this, SpyService.class);
if (!isMyServiceRunning(SpyService.class)) {
startService(mServiceIntent);
}
wasBinded = true;
bindService(mServiceIntent, this, Context.BIND_AUTO_CREATE);
}
public void reBind() {
Intent mServiceIntent = new Intent(this, SpyService.class);
wasBinded = true;
bindService(mServiceIntent, this, Context.BIND_AUTO_CREATE);
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
Log.v ("bichito", true+"<<");
return true;
}
}
return false;
}
public void restartService() {
Snackbar.make(getWindow().getDecorView(), "Parando servicio.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
doStopService();
Snackbar.make(getWindow().getDecorView(), "Reiniciando servicio.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
doStartService();
}
public void doStopService() {
// if (!isMyServiceRunning(SpyService.class)) {
// return;
// }
if (serviceCommunitcation != null && wasBinded) {
serviceCommunitcation.setCallBack(null);
unbindService(this);
wasBinded = false;
}
stopService(new Intent(this, SpyService.class));
}
public void doAfterMessage(String channelName, String eventName, final String data) {
Log.v("bichito", data);
editText.setText(data);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
// View v = findViewById(R.id.action_restart);
//noinspection SimplifiableIfStatement
if ( id == R.id.action_restart ) {
restartService();
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_setup) {
Intent o = new Intent(this, CredentialsActivity.class);
startActivity(o);
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void updateRows() {
items.clear();
items.addAll(list.getAll(this));
itemsAdapter.notifyDataSetChanged();
}
public boolean setupCredentials() {
Channel _currentChannel = new Channel();
_currentChannel.getActive(this);
if ( _currentChannel.isNonDefined() ) {
return false;
}
Event _currentEvent = new Event();
_currentEvent.getActive(this);
if ( _currentEvent.isNonDefined() ) {
return false;
}
Credential _currentCredential = new Credential();
_currentCredential.getActive(this);
if ( _currentCredential.isNonDefined()) {
return false;
}
Settings.CLIENT_CHANNEL_INFO = _currentChannel.getName();
Settings.CLIENT_EVENT_INFO = _currentEvent.getName();
Settings.CLIENT_TOKEN = _currentCredential.getToken();
Settings.CLIENT_PROVIDER = _currentCredential.getClient();
return true;
}
@Override
protected void onPause() {
super.onPause();
Log.v("bichito", "gonna pause");
// unbindService if activity is in onPause() state
if (serviceCommunitcation != null) {
serviceCommunitcation.setCallBack(null);
unbindService(this);
wasBinded = false;
}
}
@Override
protected void onStop() {
super.onStop();
Log.v("bichito", "gonna stop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("bichito", "gonna close");
}
@Override
protected void onResume() {
super.onResume();
Log.v("bichito", "on resume");
if (setupCredentials()) {
restartService();
// if ( myFirstTime ) {
// myFirstTime = false;
// restartService();
// } else {
// Intent intent = getIntent();ok
// String action = intent.getStringExtra("action");
// if ( action != null && action == "refresh" ) {
// restartService();
// } else {
// reBind();
// }
// }
} else {
Snackbar.make(getWindow().getDecorView(), "No existe datos definidos.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
//doStartService();
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
serviceCommunitcation = ((SpyService.MyBinder)service).getService();
serviceCommunitcation.setCallBack(this);
wasBinded = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onBindingDied(ComponentName name) {
}
@Override
public void onNullBinding(ComponentName name) {
}
@Override
public void onOperationProgress(int progress) {
}
@Override
public void onOperationCompleted() {
updateRows();
}
}
public class NotificationView extends Activity {
int notifyId;
TextView _title;
TextView _client;
TextView _event;
TextView _channel;
TextView _message;
TextView _extra;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notificationview);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Dismiss Notification
notificationmanager.cancel(0);
// Retrive the data from MainActivity.java
Intent i = getIntent();
try {
i.getIntExtra("notifyId", notifyId);
} catch (Exception ex){
}
Notify o = new Notify();
o.setId(notifyId);
Notify oo = o.getOne(this);
if ( oo == null ) {
return;
}
draw(oo);
}
public void draw(Notify currentNotify) {
_title = (TextView) findViewById(R.id.txtTitle);
_client = (TextView) findViewById(R.id.txtClient);
_event = (TextView) findViewById(R.id.txtEvent);
_channel = (TextView) findViewById(R.id.txtChannel);
_message = (TextView) findViewById(R.id.message);
_extra = (TextView) findViewById(R.id.extra);
_title.setText(String.format("Titulo: %s", currentNotify.getTitle()));
_client.setText(String.format("Cliente: %s", currentNotify.getToken()));
_channel.setText(String.format("Canal: %s", currentNotify.getChannel()));
_event.setText(String.format("Evento: %s", currentNotify.getEvent()));
_message.setText(String.format(currentNotify.getMessage()));
_message.setEnabled(false);
_extra.setText(String.format(currentNotify.getExtra()));
_extra.setEnabled(false);
Button btnList = (Button)findViewById(R.id.btnList);
btnList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent o = new Intent(NotificationView.this, MainActivity.class);
startActivity(o);
}
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:padding="20dp"
android:textColor="#ffffff" />
<TextView
android:id="@+id/txtEvent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:padding="20dp"
android:textColor="#ffffff" />
<TextView
android:id="@+id/txtChannel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:padding="20dp"
android:textColor="#ffffff" />
<TextView
android:id="@+id/txtClient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Mensaje" />
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mensaje"
android:paddingLeft="20dp"
android:textIsSelectable="true"
android:focusable="true"
android:longClickable="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:text="Extra" />
<TextView
android:id="@+id/extra"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:paddingLeft="20dp"
android:textIsSelectable="true"
android:focusable="true"
android:longClickable="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<Button
android:id="@+id/btnList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Listar" />
</LinearLayout>
</LinearLayout>
</ScrollView>
public class Notify {
private int id;
private String title;
private String message;
private String token;
public static DatabaseHelper dbInstance;
private String extra;
private String event;
private String channel;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getExtra() {
return extra;
}
public void setExtra(String extra) {
this.extra = extra;
}
public DatabaseHelper getDbInstance(Context _context) {
if ( Notify.dbInstance == null ) {
Notify.dbInstance = new DatabaseHelper(_context);
return Notify.dbInstance;
}
return Notify.dbInstance;
}
public Notify getOne(Context _context) {
SQLiteDatabase db = this.getDbInstance(_context).getReadableDatabase();
String[] fields = new String[] {DatabaseHelper.Columns._ID,
DatabaseHelper.Columns.COLUMN_NAME_TITLE,
DatabaseHelper.Columns.COLUMN_NAME_MESSAGE,
DatabaseHelper.Columns.COLUMN_NAME_EXTRA,
DatabaseHelper.Columns.COLUMN_NAME_CHANNEL,
DatabaseHelper.Columns.COLUMN_NAME_EVENT,
DatabaseHelper.Columns.COLUMN_NAME_TOKEN};
String[] args = new String[] {this.getId() + ""};
Cursor c = db.query(DatabaseHelper.NOTIFY_TABLE_NAME, fields,
null, null, null, null, null);
//Nos aseguramos de que existe al menos un registro
if (c.moveToFirst()) {
do {
this.setId(c.getInt(0));
this.setTitle(c.getString(1));
this.setMessage(c.getString(2));
this.setExtra(c.getString(3));
this.setChannel(c.getString(4));
this.setEvent(c.getString(5));
this.setToken(c.getString(6));
} while(c.moveToNext());
return this;
}
return null;
}
public ArrayList<Notify> getAll(Context _context) {
ArrayList<Notify> rows = new ArrayList<>();
SQLiteDatabase db = this.getDbInstance(_context).getReadableDatabase();
String[] fields = new String[] {DatabaseHelper.Columns._ID,
DatabaseHelper.Columns.COLUMN_NAME_TITLE,
DatabaseHelper.Columns.COLUMN_NAME_MESSAGE,
DatabaseHelper.Columns.COLUMN_NAME_EXTRA,
DatabaseHelper.Columns.COLUMN_NAME_TOKEN,
DatabaseHelper.Columns.COLUMN_NAME_EVENT,
DatabaseHelper.Columns.COLUMN_NAME_CHANNEL};
String[] args = new String[] {this.getId() + ""};
Cursor c = db.query(DatabaseHelper.NOTIFY_TABLE_NAME, fields,
null, null, null, null,
DatabaseHelper.Columns._ID+" DESC");
//Nos aseguramos de que existe al menos un registro
if (c.moveToFirst()) {
do {
Notify n = new Notify();
n.setId(c.getInt(0));
n.setTitle(c.getString(1));
n.setMessage(c.getString(2));
n.setExtra(c.getString(3));
n.setToken(c.getString(4));
n.setEvent(c.getString(5));
n.setChannel(c.getString(6));
rows.add(n);
} while(c.moveToNext());
}
return rows;
}
public void unsetLocal(Context _context) {
SQLiteDatabase db = this.getDbInstance(_context).getWritableDatabase();
String[] args = new String[] {this.getId() + ""};
db.delete(DatabaseHelper.NOTIFY_TABLE_NAME, "_id = ?", args);
}
public void setLocal(Context _context) {
SQLiteDatabase db = this.getDbInstance(_context).getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabaseHelper.Columns.COLUMN_NAME_TITLE, this.getTitle());
values.put(DatabaseHelper.Columns.COLUMN_NAME_MESSAGE, this.getMessage());
values.put(DatabaseHelper.Columns.COLUMN_NAME_EXTRA, this.getExtra());
values.put(DatabaseHelper.Columns.COLUMN_NAME_TOKEN, this.getToken());
values.put(DatabaseHelper.Columns.COLUMN_NAME_EVENT, this.getEvent());
values.put(DatabaseHelper.Columns.COLUMN_NAME_CHANNEL, this.getChannel());
String[] args = new String[] {this.getId() + ""};
if (this.id == 0) {
this.id = this.getAll(_context).size() + 1;
}
long newRowId = db.insert(DatabaseHelper.NOTIFY_TABLE_NAME, null, values);
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
}
public class PusherService {
SpyService spy;
Pusher pusher;
public PusherService(SpyService _spy) {
spy = _spy;
}
public void close() {
pusher.disconnect();
}
public void listen() {
PusherOptions options = new PusherOptions();
options.setCluster("us2");
pusher = new Pusher(Settings.CLIENT_TOKEN, options);
Channel channel = pusher.subscribe(Settings.CLIENT_CHANNEL_INFO);
channel.bind(Settings.CLIENT_EVENT_INFO, new SubscriptionEventListener() {
@Override
public void onEvent(final String channelName, final String eventName, final String data) {
PusherService.this.spy.pullData("Pusher", channelName,
"any", data);
// Toast.makeText(MainActivity.this, data, Toast.LENGTH_LONG).show();
}
});
pusher.connect();
}
}
public class SpyService extends Service {
private static final int NOTIFICATION_ID = 1;
public int counter = 0;
Context context;
private CallBack mCallBack;
private MyBinder mLocalbinder = new MyBinder();
AblyService ablyService;
PusherService pusherService;
public SpyService() {
}
public interface CallBack {
void onOperationProgress(int progress);
void onOperationCompleted();
}
public class MyBinder extends Binder {
public SpyService getService() {
return SpyService.this;
}
}
public void setCallBack(CallBack callBack) {
mCallBack = callBack;
}
public SpyService(Context applicationContext) {
super();
context = applicationContext;
Log.v("bichito", "aqui sse crea el servicio");
}
public void triggerCallback() {
if (mCallBack != null) {
mCallBack.onOperationCompleted();
}
}
public void Notification(int notifyId, String title, String message) {
// Open NotificationView Class on Notification Click
Intent intent = new Intent(this, NotificationView.class);
// Send data to NotificationView Class
intent.putExtra("notifyId", notifyId);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//Create Notification using NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// Set Icon
.setSmallIcon(R.mipmap.ic_launcher_foreground_avispa)
// Set Ticker Message
.setTicker(getString(R.string.avispa_notify))
// Set Title
.setContentTitle(title)
// Set Text
.setContentText(message)
// Add an Action Button below Notification
.addAction(R.drawable.ic_hongus, "Action Button", pIntent)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Dismiss Notification
.setAutoCancel(true);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
private void showForegroundNotification(String contentText) {
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("http://www.wgn.com"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setCategory(Notification.CATEGORY_PROMO)
.setContentTitle("aaaa")
.setContentText("ssss")
.setAutoCancel(true)
.setVisibility(VISIBILITY_PUBLIC)
.addAction(android.R.drawable.ic_menu_view, "View details", contentIntent)
.setContentIntent(contentIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
@Override
public IBinder onBind(Intent intent) {
return mLocalbinder;
}
public boolean setupCredentials() {
info.rayrojas.avispa.models.Channel _currentChannel = new info.rayrojas.avispa.models.Channel();
_currentChannel.getActive(this);
if ( _currentChannel.isNonDefined() ) {
return false;
}
Event _currentEvent = new Event();
_currentEvent.getActive(this);
if ( _currentEvent.isNonDefined() ) {
return false;
}
Credential _currentCredential = new Credential();
_currentCredential.getActive(this);
if ( _currentCredential.isNonDefined()) {
return false;
}
Settings.CLIENT_CHANNEL_INFO = _currentChannel.getName();
Settings.CLIENT_EVENT_INFO = _currentEvent.getName();
Settings.CLIENT_TOKEN = _currentCredential.getToken();
Settings.CLIENT_PROVIDER = _currentCredential.getClient();
return true;
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (!setupCredentials()) {
return START_STICKY;
}
if ( Settings.CLIENT_PROVIDER.equals("pusher") ) {
pusherService = new PusherService(this);
pusherService.listen();
} else if (Settings.CLIENT_PROVIDER.equals("ably") ) {
ablyService = new AblyService(this);
ablyService.listen();
}
// PusherService o = new PusherService(this);
//we have some options for service
//start sticky means service will be explicity started and stopped
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if ( ablyService != null ) {
ablyService.close();
}
if ( pusherService != null ) {
pusherService.close();
}
Log.v("bichito", "aqui sse muere el servicio");
// Intent broadcastIntent = new Intent("ac.in.ActivityRecognition.RestartSensor");
// sendBroadcast(broadcastIntent);
}
public void pullData(String client, final String channel, final String event, String data) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(data);
}catch (JSONException err){
Log.d("Error", err.toString());
return;
}
if ( jsonObject == null ) {
return;
}
String _title = client;
String _message = data;
String _extra = "-";
try {
_title = jsonObject.getString("title");
} catch (JSONException e) {
// e.printStackTrace();
}
try {
_message = jsonObject.getString("message");
} catch (JSONException e) {
// e.printStackTrace();
}
try {
_extra = jsonObject.getString("_extra");
} catch (JSONException e) {
// e.printStackTrace();
}
final String title = _title;
final String message = _message;
final String extra = _extra;
//editText.setText(data);
// showForegroundNotification(data);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Notify currentUser = new Notify();
currentUser.setId(0);
currentUser.setTitle(title);
currentUser.setMessage(message);
currentUser.setExtra(extra);
currentUser.setChannel(channel);
currentUser.setEvent(event);
currentUser.setToken(Settings.CLIENT_TOKEN);
currentUser.setLocal(SpyService.this);
SpyService.this.Notification(currentUser.getId(), title, message);
SpyService.this.triggerCallback();
// showForegroundNotification("xxxxg");
// Toast.makeText(SpyService.this, data, Toast.LENGTH_SHORT).show();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment