Skip to content

Commit

Permalink
Add calendar and account, change menu for reminders, add services
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Jamet committed Jul 26, 2017
1 parent 223bf4c commit eb8fa83
Show file tree
Hide file tree
Showing 34 changed files with 1,662 additions and 77 deletions.
44 changes: 42 additions & 2 deletions RememBirthday-UI/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.kunzisoft.remembirthday">

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<!--

<uses-permission
android:name="android.permission.AUTHENTICATE_ACCOUNTS"
android:maxSdkVersion="22" />
<uses-permission
android:name="android.permission.MANAGE_ACCOUNTS"
android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
-->

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Expand Down Expand Up @@ -45,6 +54,11 @@
android:name=".activity.SettingsActivity"
android:theme="@style/ChocolateTheme.Settings"
android:label="@string/settings_title">
<!-- required to start it from Account settings -->
<intent-filter>
<action android:name="com.kunzisoft.remembirthday.settings.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".activity.NotificationActivity"
Expand All @@ -64,6 +78,32 @@
android:enabled="true"
android:exported="false" />

<service android:name=".service.MainIntentService" />
<service
android:name=".service.AccountAuthenticatorService"
android:exported="true"
android:process=":auth"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
<service
android:name=".service.CalendarSyncAdapterService"
android:exported="true"
android:process=":calendar"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/sync_calendar" />
</service>

<receiver android:name=".notifications.NotificationEventReceiver" />
<receiver android:name=".notifications.NotificationServiceStarterReceiver">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package com.kunzisoft.remembirthday.account;

import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.app.AlarmManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Messenger;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.kunzisoft.remembirthday.service.MainIntentService;

/**
* Created by joker on 25/07/17.
*/
public class AccountResolver {

private Context context;
private Handler backgroundStatusHandler;
private String name;
private String type;
private Account account;
private String authority;

public AccountResolver(Context context,
String name,
String type,
String authority,
Handler handler) {
this.context = context;
this.name = name;
this.type = type;
this.account = new Account(name, type);
this.authority = authority;
this.backgroundStatusHandler = handler;
}

public AccountResolver(Context context,
String accountName,
String type,
String authority) {
this(context, accountName, type, authority, null);
}

/**
* Add account for Birthday Adapter to Android system
*/
public Bundle addAccountAndSync() {
Log.d(getClass().getSimpleName(), "Adding calendar account : " + account.name);

// enable automatic sync once per day
ContentResolver.setSyncAutomatically(account, authority, true);
ContentResolver.setIsSyncable(account, type, 1);

// add periodic sync interval once per day
long freq = AlarmManager.INTERVAL_DAY;
ContentResolver.addPeriodicSync(account, type, new Bundle(), freq);

AccountManager accountManager = AccountManager.get(context);
if (accountManager.addAccountExplicitly(account, null, null)) {
Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);

// Force a sync! Even when background sync is disabled, this will force one sync!
manualSync();

return result;
} else {
return null;
}
}

/**
* Remove account from Android system
*/
@SuppressWarnings("deprecation")
public boolean removeAccount() {
Log.d(getClass().getSimpleName(), "Removing account : " + account.name);

AccountManager accountManager = AccountManager.get(context);
// remove account
AccountManagerFuture accountManagerFuture;
if(android.os.Build.VERSION.SDK_INT < 23) {
accountManagerFuture = accountManager.removeAccount(account, null, null);
} else {
accountManagerFuture = accountManager.removeAccount(account, null, null, null);
}
if (accountManagerFuture.isDone()) {
try {
accountManagerFuture.getResult();
return true;
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Problem while removing account!", e);
return false;
}
} else {
return false;
}
}

/**
* Force a manual sync now!
*/
public void manualSync() {
Log.d(getClass().getSimpleName(), "Force manual synchronisation");

// Disabled: Force resync in Android OS
// Bundle extras = new Bundle();
// extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
// ContentResolver.requestSync(Constants.ACCOUNT, Constants.CONTENT_AUTHORITY, extras);

// Enabled: Force resync in own thread:
// Send all information needed to service to do in other thread
//TODO Service
Intent intent = new Intent(context, MainIntentService.class);

// Create a new Messenger for the communication back
if (backgroundStatusHandler != null) {
Messenger messenger = new Messenger(backgroundStatusHandler);
intent.putExtra(MainIntentService.EXTRA_MESSENGER, messenger);
}
intent.setAction(MainIntentService.ACTION_MANUAL_COMPLETE_SYNC);

// start service with intent
context.startService(intent);
}

/**
* Checks whether the account is enabled or not
*/
public static boolean isAccountActivated(Context context, String type, String name) {
AccountManager accountManager = AccountManager.get(context);
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
return false;
}
Account[] availableAccounts = accountManager.getAccountsByType(type);
for (Account currentAccount : availableAccounts) {
if (currentAccount.name.equals(name)) {
return true;
}
}
return false;
}

/*
@OnShowRationale(Manifest.permission.GET_ACCOUNTS)
void showRationaleForGetAccounts(final PermissionRequest request) {
new AlertDialog.Builder(context)
.setMessage(R.string.permission_get_accounts_rationale)
.setPositiveButton(R.string.button_allow, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
request.proceed();
}
})
.setNegativeButton(R.string.button_deny, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
request.cancel();
}
})
.show();
}
@OnPermissionDenied(Manifest.permission.GET_ACCOUNTS)
void showDeniedForGetAccounts() {
Toast.makeText(context, R.string.permission_get_accounts_denied, Toast.LENGTH_LONG).show();
}
@OnNeverAskAgain(Manifest.permission.GET_ACCOUNTS)
void showNeverAskForGetAccounts() {
Toast.makeText(context, R.string.permission_get_accounts_never_ask, Toast.LENGTH_LONG).show();
}
*/

public String getName() {
return name;
}

public String getType() {
return type;
}

public Account getAccount() {
return account;
}

public String getAuthority() {
return authority;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (C) 2012-2013 Dominik Schürmann <[email protected]>
*
* This file is part of Birthday Adapter.
*
* Birthday Adapter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Birthday Adapter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Birthday Adapter. If not, see <http://www.gnu.org/licenses/>.
*
*/

package com.kunzisoft.remembirthday.account;

import android.os.Handler;
import android.os.Message;

import java.lang.ref.WeakReference;

public class BackgroundStatusHandler extends Handler {

public static final int BACKGROUND_STATUS_HANDLER_DISABLE = 0;
public static final int BACKGROUND_STATUS_HANDLER_ENABLE = 1;

private WeakReference<StatusChangeListener> mListener;

public BackgroundStatusHandler(StatusChangeListener activity) {
mListener = new WeakReference<>(activity);
noOfRunningBackgroundThreads = 0;
}

private int noOfRunningBackgroundThreads;

@Override
public void handleMessage(Message msg) {
StatusChangeListener listener = mListener.get();
final int what = msg.what;

switch (what) {
case BACKGROUND_STATUS_HANDLER_ENABLE:
noOfRunningBackgroundThreads++;

if (listener != null) {
listener.onStatusChange(true);
}
break;

case BACKGROUND_STATUS_HANDLER_DISABLE:
noOfRunningBackgroundThreads--;

if (noOfRunningBackgroundThreads <= 0) {
if (listener != null) {
listener.onStatusChange(false);
}
}

break;

default:
break;
}
}

public interface StatusChangeListener {
public void onStatusChange(boolean progress);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.kunzisoft.remembirthday.account;

import android.content.Context;
import android.os.Handler;

import com.kunzisoft.remembirthday.R;

/**
* Created by joker on 25/07/17.
*/

public class CalendarAccount {

private CalendarAccount() {}

public static AccountResolver getAccount(Context context) {
return getAccount(context, null);
}

public static AccountResolver getAccount(Context context, Handler handler) {
return new AccountResolver(context,
getAccountName(context),
getAccountType(context),
getAccountAuthority(context),
handler);
}

public static boolean isAccountActivated(Context context) {
return AccountResolver.isAccountActivated(context,
getAccountType(context),
getAccountName(context));
}

public static String getAccountName(Context context) {
return context.getString(R.string.account_name);
}

public static String getAccountType(Context context) {
return context.getString(R.string.account_type);
}

public static String getAccountAuthority(Context context) {
return context.getString(R.string.account_authority);
}
}
Loading

0 comments on commit eb8fa83

Please sign in to comment.