changement de candidat à l'investissement

This commit is contained in:
Paul Schneider 2017-01-26 11:04:03 +01:00
commit be3087fff6
351 changed files with 359 additions and 497 deletions

View file

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using BookAStar.Droid.OAuth;
namespace BookAStar.Droid.Services
{
[Service(
Name = "fr.pschneider.bas.AccountChooserService",
Label = "Yavsc accounts service",
Icon = "@drawable/icon",
Exported = true,
Enabled = true
)]
[IntentFilter(new String[] { "android.accounts.AccountAuthenticator" })]
class AccountChooserService : Service
{
public static YaOAuth2Authenticator authenticator;
public override void OnCreate()
{
base.OnCreate();
}
public override IBinder OnBind(Intent intent)
{
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using BookAStar.Droid.Interfaces;
using Newtonsoft.Json;
using BookAStar.Model.Social;
using BookAStar.Data;
using BookAStar.Model;
namespace BookAStar.Droid.Services.GCMHandlers
{
class BookQueryGCMHandler : GCMessageHandler
{
public BookQueryGCMHandler(Context context,
NotificationManager manager,
Notification.Builder builder) : base(context,manager,builder)
{
}
/// <summary>
/// Prend en charge le message push
/// contenant une nouvelle demande de rendez-vous
/// </summary>
/// <param name="from"></param>
/// <param name="data"></param>
public override void Handle(string from, Bundle data)
{
var locationJson = data.GetString("Location");
var location = JsonConvert.DeserializeObject<Location>(locationJson);
var cid = long.Parse(data.GetString("Id"));
var clientJson = data.GetString("Client");
var client = JsonConvert.DeserializeObject<ClientProviderInfo>(clientJson);
var bq = new BookQuery
{
Id = cid,
Location = location,
Client = client,
Reason = data.GetString("Reason")
};
var dateString = data.GetString("EventDate");
DateTime evDate;
if (DateTime.TryParse(dateString, out evDate))
{
bq.EventDate = evDate;
}
SendBookQueryNotification(bq);
}
/// <summary>
/// Notifie la demande
/// </summary>
/// <param name="bquery"></param>
void SendBookQueryNotification(BookQuery bquery)
{
DataManager.Instance.BookQueries.Merge(bquery);
var bookquerynotifications = DataManager.Instance.BookQueries.Where(
q => !q.Read && q.EventDate > DateTime.Now
).ToArray();
var count = bookquerynotifications.Length;
var multiple = count > 1;
var title =
multiple ? $"{count} demandes" : bquery.Client.UserName;
var message = $"{bquery.EventDate} {bquery.Client.UserName} {bquery.Location.Address}\n {bquery.Reason}";
var intent = new Intent(context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("BookQueryId", bquery.Id);
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
int maxil = 5;
for (int cn = 0; cn < count && cn < maxil; cn++)
{
inboxStyle.AddLine(bookquerynotifications[cn].Client.UserName);
}
if (count > maxil)
inboxStyle.SetSummaryText($"Plus {count - maxil} autres");
else inboxStyle.SetSummaryText((string)null);
notificationBuilder.SetContentTitle(title).SetContentText(message)
.SetStyle(inboxStyle)
.SetContentIntent(pendingIntent);
var notification = notificationBuilder.Build();
notificationManager.Notify(bookQueryNotificationId, notification);
}
int bookQueryNotificationId = 1;
}
}

View file

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Newtonsoft.Json;
using BookAStar.Model.Social;
using BookAStar.Model;
using BookAStar.Model.Workflow;
using BookAStar.Data;
namespace BookAStar.Droid.Services.GCMHandlers
{
class EstimateGCMHandler: GCMessageHandler
{
public EstimateGCMHandler(Context context,
NotificationManager manager,
Notification.Builder builder) :
base(context,manager,builder)
{
}
public override void Handle(string from, Bundle data)
{
var locationJson = data.GetString("Location");
var location = JsonConvert.DeserializeObject<Location>(locationJson);
var eid = long.Parse(data.GetString("Id"));
var clientJson = data.GetString("Client");
var client = JsonConvert.DeserializeObject<ClientProviderInfo>(clientJson);
var estimate = new Estimate
{
Id = eid
};
var dateString = data.GetString("ProviderValidationDate");
DateTime evDate;
if (DateTime.TryParse(dateString, out evDate))
{
estimate.ProviderValidationDate = evDate;
}
Notify(estimate);
}
void Notify (Estimate estimate)
{
// do merge the data, even when no user is active
DataManager.Instance.Estimates.Merge(estimate);
if (MainSettings.CurrentUser == null) return;
var estimatenotifications = DataManager.Instance.Estimates.Where(
e => e.ClientApprouvalDate == default(DateTime) &&
e.ClientId == MainSettings.CurrentUser.Id
).OrderByDescending(e=>e.ProviderValidationDate).ToArray();
var count = estimatenotifications.Length;
var multiple = count > 1;
string title;
string message;
if (multiple)
{
StringBuilder tb = new StringBuilder();
int nc = 0;
foreach (var pro in estimatenotifications.Select(
e=>e.Owner
).Distinct())
{
nc++;
tb.Append($"{pro.UserName}");
if (nc > 3)
{
tb.Append(" et {count-nc} autres");
break;
}
else tb.Append(", ");
}
tb.Append("attendent votre validation de leur devis");
title = tb.ToString();
message =
string.Join("\n",
estimatenotifications.Select(
n => $"{n.Title} [{n.Owner.UserName}]\n").ToArray());
}
else
{
title = $"{estimate.Owner.UserName} attend votre validation de son devis";
message = $"{estimate.Title} ({estimate.Total} euro)\n({estimate.Query.Reason})";
}
var intent = new Intent(context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("EstimateId", estimate.Id);
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
int maxil = 5;
for (int cn = 0; cn < count && cn < maxil; cn++)
{
inboxStyle.AddLine(estimatenotifications[cn].Owner.UserName);
}
if (count > maxil)
inboxStyle.SetSummaryText($"Plus {count - maxil} autres");
else inboxStyle.SetSummaryText((string)null);
notificationBuilder.SetContentTitle(title).SetContentText(message)
.SetStyle(inboxStyle)
.SetContentIntent(pendingIntent);
var notification = notificationBuilder.Build();
notificationManager.Notify(notificationId, notification);
}
int notificationId = 2;
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using BookAStar.Droid.Interfaces;
namespace BookAStar.Droid.Services.GCMHandlers
{
abstract class GCMessageHandler : IGCMessageHandler
{
protected Context context;
protected NotificationManager notificationManager;
protected Notification.Builder notificationBuilder;
public GCMessageHandler(Context context,
NotificationManager notificationManager,
Notification.Builder notificationBuilder)
{
this.context = context;
this.notificationBuilder = notificationBuilder;
this.notificationManager = notificationManager;
}
public abstract void Handle(string from, Bundle data);
}
}

View file

@ -0,0 +1,82 @@
using Android.App;
using Android.Content;
using Android.OS;
using Android.Gms.Gcm;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace BookAStar.Droid.Services
{
using Model.Social;
using Model;
using Data;
using Interfaces;
using GCMHandlers;
namespace ClientApp
{
[Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
public class MyGcmListenerService : GcmListenerService
{
private Notification.Builder notificationBuilder;
NotificationManager notificationManager;
Dictionary<string, IGCMessageHandler> Handlers;
public override void OnCreate()
{
base.OnCreate();
notificationBuilder = new Notification.Builder(this)
.SetSmallIcon(Resource.Drawable.icon)
.SetAutoCancel(true);
notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
Handlers = new Dictionary<string, IGCMessageHandler>
{
{"BookQuery", new BookQueryGCMHandler(this,notificationManager,notificationBuilder) }
};
}
public override void OnDestroy()
{
base.OnDestroy();
notificationManager.Dispose();
notificationManager = null;
notificationBuilder.Dispose();
notificationBuilder = null;
}
public override void OnMessageReceived(string from, Bundle data)
{
var topic = data.GetString("Topic");
if (Handlers.ContainsKey(topic))
{
Handlers[topic].Handle(from, data);
}
else
{
throw new NotImplementedException(topic);
}
}
/* TODO cleaning
void SendNotification(string title, string message)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Notification.Builder(this)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
notificationManager.Notify(0, notificationBuilder.Build());
}*/
}
}
}

View file

@ -0,0 +1,112 @@
using System;
using Android.App;
using Android.Content;
using Android.Util;
using System.Net;
using System.IO;
using Android.Gms.Gcm;
using Android.Gms.Gcm.Iid;
using Android.OS;
using Android;
namespace BookAStar.Droid
{
[Service(Exported = false)]
class GcmRegistrationIntentService : IntentService
{
static object locker = new object();
public GcmRegistrationIntentService() : base("RegistrationIntentService") {
}
static PowerManager.WakeLock sWakeLock;
static object LOCK = new object();
public override void OnCreate()
{
base.OnCreate();
sWakeLock = PowerManager.FromContext(this).NewWakeLock(WakeLockFlags.Partial,
"BookAStar");
sWakeLock.Acquire();
}
public override void OnDestroy()
{
base.OnDestroy();
sWakeLock.Release();
}
protected override void OnHandleIntent (Intent intent)
{
try
{
Log.Info ("RegistrationIntentService", "Calling InstanceID.GetToken");
lock (locker)
{
var instanceID = InstanceID.GetInstance(this);
#if DEBUG
// When debugging, and application data/cache is preserved,
// a previous instance comes from another application installation
// and the old registration against GCM fails,
// until one delete it.
try
{
instanceID.DeleteInstanceID();
}
catch(Exception ex)
{
Debug.WaitForDebugger();
Log.Debug("bas.GCM", ex.StackTrace.ToString());
}
#endif
var senderid = MainSettings.GoogleSenderId;
var token = instanceID.GetToken ( senderid,
GoogleCloudMessaging.InstanceIdScope, null);
Log.Info ("RegistrationIntentService", "GCM Registration Token: " + token);
SendRegistrationToAppServer (token);
Subscribe (token);
}
}
catch (WebException e) {
Log.Debug ("RegistrationIntentService", "Failed to get a registration token");
if (e.Response!=null)
using (var s = e.Response.GetResponseStream ()) {
using (var r = new StreamReader (s)) {
var t = r.ReadToEnd ();
Log.Debug("RegistrationIntentService",t);
}
}
return;
}
catch (Exception e)
{
Log.Error ("RegistrationIntentService", "Failed to get a registration token");
Log.Error ("RegistrationIntentService", e.Message);
return;
}
}
void SendRegistrationToAppServer (string token)
{
MainSettings.GoogleRegId = token;
}
void Subscribe (string token)
{
var pubSub = GcmPubSub.GetInstance(this);
pubSub.Subscribe(token, "/topics/global", null);
// TODO if a Activity is specified,
// and general annonces in this activity are accepted:
//
// pubSub.Subscribe(token, "/topics/jobs/"+ActivityCode, null);
}
}
}

View file

@ -0,0 +1,134 @@
using System;
using Android.App;
using Android.OS;
using Android.Content;
using Android.Util;
using Android.Widget;
namespace BookAStar.Droid
{
[Service]
public class MyGcmIntentService : IntentService
{
static PowerManager.WakeLock sWakeLock;
static object LOCK = new object();
public static void RunIntentInService(Context context, Intent intent)
{
lock (LOCK)
{
if (sWakeLock == null)
{
// This is called from BroadcastReceiver, there is no init.
var pm = PowerManager.FromContext(context);
sWakeLock = pm.NewWakeLock(
WakeLockFlags.Partial, "My WakeLock Tag");
}
}
sWakeLock.Acquire();
intent.SetClass(context, typeof(MyGcmIntentService));
context.StartService(intent);
}
static object locker = new object();
protected override void OnHandleIntent(Intent intent)
{
try
{
Log.Info ("MyIntentService", "Calling InstanceID.GetToken");
lock (locker)
{
string action = intent.Action;
if (action!=null)
if (action.Equals("com.google.android.c2dm.intent.REGISTRATION"))
{
HandleRegistration(intent);
}
else if (action.Equals("com.google.android.c2dm.intent.RECEIVE"))
{
HandleMessage(intent);
}
}
}
finally
{
lock (LOCK)
{
//Sanity check for null as this is a public method
if (sWakeLock != null)
sWakeLock.Release();
}
}
}
private void HandleMessage(Intent intent)
{
// get the notification type id:
string ntft = intent.GetStringExtra("type");
string msg = intent.GetStringExtra("Description");
var position = intent.GetSerializableExtra ("Location");
SendNotification (msg);
}
void SendNotification (string message)
{
/* Bundle valuesForActivity = new Bundle();
valuesForActivity.PutInt("count", count); */
var intent = new Intent (this, typeof(MainActivity));
intent.AddFlags (ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity (this, 0, intent, PendingIntentFlags.OneShot);
// Construct a back stack for cross-task navigation:
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
stackBuilder.AddNextIntent(intent);
// Create the PendingIntent with the back stack:
PendingIntent resultPendingIntent =
stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);
var notificationBuilder = new Notification.Builder(this)
.SetAutoCancel(true)
.SetSmallIcon (Resource.Drawable.icon)
.SetContentTitle ("GCM Message")
.SetContentText (message)
.SetContentIntent(resultPendingIntent) // Start 2nd activity when the intent is clicked.
;
var notificationManager = (NotificationManager) GetSystemService(Context.NotificationService);
notificationManager.Notify (0, notificationBuilder.Build());
}
private void HandleRegistration(Intent intent)
{
string registrationId = intent.GetStringExtra("registration_id");
string error = intent.GetStringExtra("error");
string unregistration = intent.GetStringExtra("unregistered");
}
void SubscribeGCM ()
{
Context context = this.ApplicationContext;
string senders = MainSettings.GoogleSenderId;
// Resources.GetString(GoogleSenderId);
Intent intent = new Intent ("com.google.android.c2dm.intent.REGISTER");
intent.SetPackage ("com.google.android.gsf");
intent.PutExtra ("app", PendingIntent.GetBroadcast (context, 0, new Intent (), 0));
intent.PutExtra ("sender", senders);
context.StartService (intent);
}
void UnsubscribeGCM ()
{
Context context = this.ApplicationContext;
Intent intent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
intent.PutExtra("app", PendingIntent.GetBroadcast(context, 0, new Intent(), 0));
context.StartService (intent);
}
}
}

View file

@ -0,0 +1,18 @@
using System;
using Android.App;
using Android.Gms.Gcm.Iid;
using Android.Content;
namespace BookAStar.Droid
{
[Service(Exported = false), IntentFilter(new[] { "com.google.android.gms.iid.InstanceID" })]
class MyInstanceIDListenerService : InstanceIDListenerService
{
public override void OnTokenRefresh()
{
var intent = new Intent (this, typeof (GcmRegistrationIntentService));
StartService (intent);
}
}
}

View file

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Service.Chooser;
using static Android.Manifest;
namespace BookAStar.Droid
{
[Service(
Name = "fr.pschneider.bas.YavscChooserTargetService",
Label = "Yavsc share service",
Permission = Permission.BindChooserTargetService,
Icon = "@drawable/icon",
Exported = true,
Enabled = true
)]
[IntentFilter(new String[] { "android.service.chooser.ChooserTargetService" })]
class YavscChooserTargetService : ChooserTargetService
{
public override IList<ChooserTarget> OnGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter)
{
Android.Graphics.Drawables.Icon i =
Android.Graphics.Drawables.Icon.CreateWithResource(this.BaseContext,
Resource.Drawable.icon);
ChooserTarget t = new ChooserTarget(
new Java.Lang.String(
Constants.ApplicationName), i,
.5f, new ComponentName(this, "BookAStar.SendFilesActivity"),
null);
var res = new List<ChooserTarget>();
res.Add(t);
return res;
}
public override IBinder OnBind(Intent intent)
{
return base.OnBind(intent);
}
}
}