-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.cs
More file actions
149 lines (136 loc) · 4.92 KB
/
Copy pathMainActivity.cs
File metadata and controls
149 lines (136 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using System.Threading;
using BroadCast.DataProviders;
using BroadCast.Web;
using ZXing.Mobile;
using Newtonsoft.Json.Linq;
using System;
using Android.Support.V4.Content;
using Android.Content.PM;
using Android.Support.V4.App;
using System.Collections.Generic;
using Android.Widget;
namespace BroadCast
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
EnsurePermissions();
}
private void init()
{
ThreadPool.QueueUserWorkItem(o =>
{
ImageAlbums albums = new ImageAlbums();
DataContainer.SetAlbums(albums);
Server server = new Server();
SimpleSocketServer simpleSocket = new SimpleSocketServer(server.wss);
DataContainer.NewImageSelected += new EventHandler((object sender, EventArgs e) =>
{
simpleSocket.Send("image", ((DataContainer.NewImageSelectedEventArgs)e).remotePath);
});
DataContainer.ImageViewActvitiyClosed += new EventHandler((object sender, EventArgs e) =>
{
simpleSocket.Send("albums");
});
simpleSocket.onClose(() =>
{
// TODO close app
});
Connect();
});
}
private void DisplayMissingPermissionsMessage()
{
TextView message = FindViewById<TextView>(Resource.Id.message);
message.Text = GetString(Resource.String.permissions_denied);
}
private void HideMissingPermissionsMessage()
{
TextView message = FindViewById<TextView>(Resource.Id.message);
message.Text = "";
}
private List<string> missingPermissions;
private void EnsurePermissions()
{
missingPermissions = new List<string> { };
foreach (string permission in Helpers.Constants.REQUIRED_PERMISSIONS)
{
if (ContextCompat.CheckSelfPermission(this, permission) != (int)Permission.Granted)
{
missingPermissions.Add(permission);
}
}
if (missingPermissions.Count > 0)
{
DisplayMissingPermissionsMessage();
ActivityCompat.RequestPermissions(this, missingPermissions.ToArray(), Helpers.Constants.PERMISSION_REQUEST);
}
else
{
init();
}
}
public void Connect()
{
ThreadPool.QueueUserWorkItem(async o =>
{
MobileBarcodeScanner.Initialize(Application);
var scanner = new ZXing.Mobile.MobileBarcodeScanner();
var result = await scanner.Scan();
if (result == null)
{
return;
}
JObject qr = JObject.Parse(result.Text);
WebApp.Connect((string)qr["uuid"], (string)qr["wsUrl"], () =>
{
RunOnUiThread(() =>
{
StartActivity(typeof(Activities.AlbumViewActivity));
});
});
});
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
if (requestCode == Helpers.Constants.PERMISSION_REQUEST)
{
if (grantResults.Length == missingPermissions.Count)
{
bool permissionsGranted = true;
for (int grantIndex = 0; grantIndex < grantResults.Length - 1; grantIndex++)
{
if (grantResults[grantIndex] != Permission.Granted)
{
permissionsGranted = false;
break;
}
}
if (permissionsGranted)
{
HideMissingPermissionsMessage();
init();
}
else
{
DisplayMissingPermissionsMessage();
}
}
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
}