Skip to content

Commit

Permalink
+ add permissionCheck with Android >=23
Browse files Browse the repository at this point in the history
+ set Target Version to 23
+ remove depricated Notification implementation
  • Loading branch information
Longri committed May 12, 2016
1 parent a82f841 commit 09f8692
Show file tree
Hide file tree
Showing 6 changed files with 545 additions and 432 deletions.
2 changes: 1 addition & 1 deletion Android_GUI/AndroidManifest.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.cachebox_test" android:installLocation="auto"
android:versionCode="20160419" android:versionName="0.8.20160419(Test)">
android:versionCode="20160512" android:versionName="0.8.20160512(Test)">
<uses-sdk android:minSdkVersion="12" android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
Expand Down
2 changes: 1 addition & 1 deletion Android_GUI/project.properties
Expand Up @@ -8,5 +8,5 @@
# project structure.

# Project target.
target=android-21
target=android-23

280 changes: 137 additions & 143 deletions Android_GUI/src/de/cachebox_test/NotifyService.java
Expand Up @@ -2,7 +2,6 @@

import CB_Locator.Events.GPS_FallBackEvent;
import CB_Locator.Events.GPS_FallBackEventList;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
Expand All @@ -12,162 +11,157 @@

public class NotifyService extends Service implements GPS_FallBackEvent {

int mStartMode; // indicates how to behave if the service is killed
boolean mAllowRebind; // indicates whether onRebind should be used
int mStartMode; // indicates how to behave if the service is killed
boolean mAllowRebind; // indicates whether onRebind should be used

final static int myID = 1234;
final static int myID = 1234;

// Binder wird von der ServiceConnection verwendet
private final IBinder mBinder = new LocalBinder();
// Binder wird von der ServiceConnection verwendet
private final IBinder mBinder = new LocalBinder();

/**
* Usere verschachtelte Klasse
*/
class LocalBinder extends Binder {

/**
* Usere verschachtelte Klasse
* Damit erhalten wir Zugriff auf unseren Service. Gibt unsere Instanz des NotifyService zurück.
*/
class LocalBinder extends Binder {

/**
* Damit erhalten wir Zugriff auf unseren Service. Gibt unsere Instanz des NotifyService zurück.
*/
NotifyService getService() {
return NotifyService.this;
}
}

@Override
public void onCreate() {
// The service is being created
super.onCreate();
GPS_FallBackEventList.Add(this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The service is starting, due to a call to startService()
return mStartMode;
}

@SuppressWarnings("deprecation")
@Override
public IBinder onBind(Intent intent) {
// A client is binding to the service with bindService()

Intent mainIntent = new Intent(this, main.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);

// This constructor is deprecated. Use Notification.Builder instead
Notification notice = new Notification(R.drawable.cb_icon_0, "Cachebox", System.currentTimeMillis());

// This method is deprecated. Use Notification.Builder instead.
notice.setLatestEventInfo(this, "Cachebox", "is running", pendIntent);

// notice.flags |= Notification.FLAG_NO_CLEAR;

// TODO no Clear wieder einschalten => runNotification.flags |= Notification.FLAG_NO_CLEAR;

startForeground(myID, notice);

return mBinder;
}

public static boolean finish = false;

@SuppressWarnings("deprecation")
@Override
public boolean onUnbind(Intent intent) {
// All clients have unbound with unbindService()

if (finish) {
// CB is closing from User
stopForeground(true);
} else {
// CB is killing
Log.d("CACHEBOX", "Service => ACB is killed");
Intent mainIntent = new Intent(this, main.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);

// This constructor is deprecated. Use Notification.Builder instead
Notification notice = new Notification(R.drawable.cb_killed, "Cachebox", System.currentTimeMillis());

// This method is deprecated. Use Notification.Builder instead.
notice.setLatestEventInfo(this, "Cachebox", "was killing", pendIntent);

// notice.flags |= Notification.FLAG_NO_CLEAR;

// TODO no Clear wieder einschalten => runNotification.flags |= Notification.FLAG_NO_CLEAR;

startForeground(myID, notice);

}

return mAllowRebind;
}

@Override
public void onRebind(Intent intent) {
// A client is binding to the service with bindService(),
// after onUnbind() has already been called
NotifyService getService() {
return NotifyService.this;
}
}

@Override
public void onCreate() {
// The service is being created
super.onCreate();
GPS_FallBackEventList.Add(this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The service is starting, due to a call to startService()
return mStartMode;
}

@Override
public IBinder onBind(Intent intent) {
// A client is binding to the service with bindService()

Intent mainIntent = new Intent(this, main.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);

// // This constructor is deprecated. Use Notification.Builder instead
// Notification notice = new Notification(R.drawable.cb_icon_0, "Cachebox", System.currentTimeMillis());
//
// // This method is deprecated. Use Notification.Builder instead.
// notice.setLatestEventInfo(this, "Cachebox", "is running", pendIntent);
//
// // notice.flags |= Notification.FLAG_NO_CLEAR;
//
// // TODO no Clear wieder einschalten => runNotification.flags |= Notification.FLAG_NO_CLEAR;
//
// startForeground(myID, notice);

return mBinder;
}

public static boolean finish = false;

@Override
public boolean onUnbind(Intent intent) {
// All clients have unbound with unbindService()

if (finish) {
// CB is closing from User
stopForeground(true);
} else {
// CB is killing
Log.d("CACHEBOX", "Service => ACB is killed");
Intent mainIntent = new Intent(this, main.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);

// // This constructor is deprecated. Use Notification.Builder instead
// Notification notice = new Notification(R.drawable.cb_killed, "Cachebox", System.currentTimeMillis());
//
// // This method is deprecated. Use Notification.Builder instead.
// notice.setLatestEventInfo(this, "Cachebox", "was killing", pendIntent);
//
// // notice.flags |= Notification.FLAG_NO_CLEAR;
//
// // TODO no Clear wieder einschalten => runNotification.flags |= Notification.FLAG_NO_CLEAR;
//
// startForeground(myID, notice);

@SuppressWarnings("deprecation")
@Override
public void onDestroy() {
// The service is no longer used and is being destroyed

if (!finish) {
Log.d("CACHEBOX", "Service => ACB is killed");
Intent mainIntent = new Intent(this, main.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);

// This constructor is deprecated. Use Notification.Builder instead
Notification notice = new Notification(R.drawable.cb_killed, "Cachebox", System.currentTimeMillis());

// This method is deprecated. Use Notification.Builder instead.
notice.setLatestEventInfo(this, "Cachebox", "was killing", pendIntent);

}
}

@SuppressWarnings("deprecation")
@Override
public void FallBackToNetworkProvider() {
Intent mainIntent = new Intent(this, main.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);

// This constructor is deprecated. Use Notification.Builder instead
Notification notice = new Notification(R.drawable.cb_icon_0, "Cachebox", System.currentTimeMillis());

// This method is deprecated. Use Notification.Builder instead.
notice.setLatestEventInfo(this, "Cachebox", "is running", pendIntent);

// notice.flags |= Notification.FLAG_NO_CLEAR;

// TODO no Clear wieder einschalten => runNotification.flags |= Notification.FLAG_NO_CLEAR;

startForeground(myID, notice);
}

@SuppressWarnings("deprecation")
@Override
public void Fix() {
Intent mainIntent = new Intent(this, main.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);
return mAllowRebind;
}

// This constructor is deprecated. Use Notification.Builder instead
Notification notice = new Notification(R.drawable.cb_icon_1, "Cachebox", System.currentTimeMillis());
@Override
public void onRebind(Intent intent) {
// A client is binding to the service with bindService(),
// after onUnbind() has already been called
}

// This method is deprecated. Use Notification.Builder instead.
notice.setLatestEventInfo(this, "Cachebox", "is running", pendIntent);
@Override
public void onDestroy() {
// The service is no longer used and is being destroyed

// notice.flags |= Notification.FLAG_NO_CLEAR;
if (!finish) {
Log.d("CACHEBOX", "Service => ACB is killed");
Intent mainIntent = new Intent(this, main.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);

// TODO no Clear wieder einschalten => runNotification.flags |= Notification.FLAG_NO_CLEAR;
// // This constructor is deprecated. Use Notification.Builder instead
// Notification notice = new Notification(R.drawable.cb_killed, "Cachebox", System.currentTimeMillis());
//
// // This method is deprecated. Use Notification.Builder instead.
// notice.setLatestEventInfo(this, "Cachebox", "was killing", pendIntent);

startForeground(myID, notice);
}
}

@Override
public void FallBackToNetworkProvider() {
Intent mainIntent = new Intent(this, main.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);

// // This constructor is deprecated. Use Notification.Builder instead
// Notification notice = new Notification(R.drawable.cb_icon_0, "Cachebox", System.currentTimeMillis());
//
// // This method is deprecated. Use Notification.Builder instead.
// notice.setLatestEventInfo(this, "Cachebox", "is running", pendIntent);
//
// // notice.flags |= Notification.FLAG_NO_CLEAR;
//
// // TODO no Clear wieder einschalten => runNotification.flags |= Notification.FLAG_NO_CLEAR;
//
// startForeground(myID, notice);
}

@Override
public void Fix() {
Intent mainIntent = new Intent(this, main.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);

// // This constructor is deprecated. Use Notification.Builder instead
// Notification notice = new Notification(R.drawable.cb_icon_1, "Cachebox", System.currentTimeMillis());
//
// // This method is deprecated. Use Notification.Builder instead.
// notice.setLatestEventInfo(this, "Cachebox", "is running", pendIntent);
//
// // notice.flags |= Notification.FLAG_NO_CLEAR;
//
// // TODO no Clear wieder einschalten => runNotification.flags |= Notification.FLAG_NO_CLEAR;
//
// startForeground(myID, notice);
}

}
58 changes: 58 additions & 0 deletions Android_GUI/src/de/cachebox_test/PermissionCheck.java
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2016 team-cachebox.de
*
* Licensed under the : GNU General Public License (GPL);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.cachebox_test;

import java.util.ArrayList;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
* Created by Longri on 11.05.2016.
*/
public class PermissionCheck {

public static final int MY_PERMISSIONS_REQUEST = 11052016;
static final String WAKE_LOCK = "android.permission.WAKE_LOCK";
static final String INTERNET = "android.permission.INTERNET";
static final String ACCESS_NETWORK_STATE = "android.permission.ACCESS_NETWORK_STATE";
static final String RECORD_AUDIO = "android.permission.RECORD_AUDIO";
static final String CAMERA = "android.permission.CAMERA";
static final String VIBRATE = "android.permission.VIBRATE";
static final String WRITE_EXTERNAL_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE";
static final String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE";

static final String[] NEEDED_PERMISSIONS = new String[] { WAKE_LOCK, INTERNET, ACCESS_NETWORK_STATE, RECORD_AUDIO, CAMERA, VIBRATE, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE };

public static void checkNeededPermissions(Activity context) {
ArrayList<String> DENIED_List = new ArrayList<String>();

for (String permission : NEEDED_PERMISSIONS) {
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
DENIED_List.add(permission);
}
}

if (!DENIED_List.isEmpty()) {
String[] ar = DENIED_List.toArray(new String[DENIED_List.size()]);
ActivityCompat.requestPermissions(context, ar, MY_PERMISSIONS_REQUEST);
}

}

}

0 comments on commit 09f8692

Please sign in to comment.