-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileImportActivity.cs
More file actions
150 lines (136 loc) · 4.54 KB
/
FileImportActivity.cs
File metadata and controls
150 lines (136 loc) · 4.54 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
150
///
/// WF.Player.Android - A Wherigo Player for Android, which use the Wherigo Foundation Core.
/// Copyright (C) 2012-2014 Dirk Weltz <mail@wfplayer.com>
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Lesser General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License, or (at your option) any later version.
///
/// This program 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 Lesser General Public License for more details.
///
/// You should have received a copy of the GNU Lesser General Public License
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
///
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace WF.Player
{
[Activity (Label = "WF.Player", Theme="@android:style/Theme.NoTitleBar")]
// For e-mail
[IntentFilter (new[]{Intent.ActionView},
Categories=new[]{Intent.CategoryOpenable, Intent.CategoryLauncher, Intent.CategoryDefault, Intent.CategoryBrowsable},
Icon="@drawable/icon",
DataScheme="content",
DataPathPattern=@".*\\.gwc",
DataMimeType="application/octet-stream")]
// // For http
// [IntentFilter (new[]{Intent.ActionView},
// Categories=new[]{Intent.CategoryDefault, Intent.CategoryBrowsable},
// Icon="@drawable/icon",
// DataScheme="http",
// DataHost="*",
// DataPathPattern=@".*\\.gwc",
// DataMimeType="*/*")]
// // For https
// [IntentFilter (new[]{Intent.ActionView},
// Categories=new[]{Intent.CategoryOpenable, Intent.CategoryLauncher, Intent.CategoryDefault, Intent.CategoryBrowsable},
// Icon="@drawable/icon",
// DataScheme="https",
// DataHost="*",
// DataPathPattern=@".*\\.gwc",
// DataMimeType="*/*")]
// For files
[IntentFilter (new[]{Intent.ActionView},
Categories=new[]{Intent.CategoryDefault},
Icon="@drawable/icon",
DataScheme="file",
DataHost="*",
DataPathPattern=@".*\\.gwc",
DataMimeType="*/*")]
public class FileImportActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Get adress of file from intent
Android.Net.Uri data = this.Intent.Data;
// If there are any information
if (data != null) {
// Clear data, because we have it allready in data
this.Intent.SetData(null);
var cguid = data.GetQueryParameter("CGUID");
// Try to import the data
try {
string fileName = ImportData(data);
if (fileName != null) {
} else {
Finish();
return;
}
}
catch (Exception e) {
// warn user about bad data here
Finish();
return;
}
// launch MainActivity (with FLAG_ACTIVITY_CLEAR_TOP)
Intent intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
intent.AddFlags(ActivityFlags.ClearTop);
StartActivity(intent);
}
}
string ImportData(Android.Net.Uri uri)
{
string scheme = uri.Scheme;
if(ContentResolver.SchemeContent.Equals(scheme) || ContentResolver.SchemeFile.Equals(scheme)) {
// Now we have the path and filename of the new file
string fileName = GetFileNameByUri(this, uri);
var fis = new BinaryReader(ContentResolver.OpenInputStream(uri));
var fos = new FileStream(Path.Combine(Main.Path, fileName), FileMode.CreateNew);
fis.BaseStream.CopyTo(fos);
fos.Close();
fis.Close();
return fileName;
}
return null;
}
string GetFileNameByUri(Context context, Android.Net.Uri uri)
{
string fileName = "unknown";
Android.Net.Uri filePathUri = uri;
if (ContentResolver.SchemeContent.Equals(uri.Scheme))
{
Android.Database.ICursor cursor = ContentResolver.Query(uri, null, null, null, null);
if (cursor.MoveToFirst())
{
int column_index = cursor.GetColumnIndexOrThrow("_data");
filePathUri = Android.Net.Uri.Parse(cursor.GetString(column_index));
fileName = filePathUri.LastPathSegment.ToString();
}
}
else if (ContentResolver.SchemeFile.Equals(uri.Scheme))
{
fileName = filePathUri.LastPathSegment.ToString();
}
else
{
fileName = fileName+"_"+filePathUri.LastPathSegment;
}
return fileName;
}
}
}