Import inital de l'application mobile Xamarin

This commit is contained in:
Paul Schneider 2016-07-20 12:33:16 +02:00
commit 0d8d159285
157 changed files with 13250 additions and 0 deletions

View file

@ -0,0 +1,91 @@
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);
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);
pubSub.Subscribe (token, "/topics/jobs", null);
}
}
}

View file

@ -0,0 +1,122 @@
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)
{
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 ("GCM Message")
.SetContentText (message)
.SetAutoCancel (true)
.SetContentIntent (pendingIntent);
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 = "325408689282";
// 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);
}
}
}