Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ImportExternalContentActivity.cs
浏览该文件的文档.
1using Android.App;
2using Android.Content;
3using Android.Content.PM;
4using Android.Database;
5using Android.OS;
6using Android.Provider;
7using Android.Util;
8using Android.Views;
9using Android.Widget;
10using Engine;
11using Game;
12using Resource = _Microsoft.Android.Resource.Designer.Resource;
13using Uri = Android.Net.Uri;
14
15#pragma warning disable CA1416
16namespace SC4Android {
17 [Activity(
18 Label = "@string/ImportExternalContentActivityLabel",
19 LaunchMode = LaunchMode.SingleTop,
20 Icon = "@mipmap/icon",
21 Theme = "@style/ImportExternalContentDialog",
22 Exported = true
23 ),
24 IntentFilter(
25 ["android.intent.action.VIEW"],
26 DataSchemes = ["file", "content"],
27 DataMimeTypes = ["*/*", "image/png", "image/webp"],
28 DataPathPatterns = [
29 @".*\.scworld",
30 @".*\.scbtex",
31 @".*\.scskin",
32 @".*\.scfpack",
33 @".*\.scmod",
34 @".*\.png",
35 @".*\.webp",
36 @".*\.astc",
37 @".*\.astcsrgb"
38 ],
39 Categories = ["android.intent.category.DEFAULT", "android.intent.category.BROWSABLE"]
40 ), IntentFilter(["android.intent.action.SEND"], DataMimeType = "*/*", Categories = ["android.intent.category.DEFAULT"])]
41 public class ImportExternalContentActivity : Activity {
43
44 protected override void OnCreate(Bundle savedInstanceState) {
45 base.OnCreate(savedInstanceState);
46 Intent intent = Intent;
47 if (intent == null) {
48 Toast.MakeText(this, Resources?.GetString(Resource.String.FileNotFound), ToastLength.Short)?.Show();
49 FinishAndRemoveTask();
50 return;
51 }
52 Uri uri = null;
53 if (intent.Action == Intent.ActionView) {
54 uri = intent.Data;
55 }
56 else if (intent.Action == Intent.ActionSend) {
57 if (Build.VERSION.SdkInt >= (BuildVersionCodes)33) {
58 uri = intent.GetParcelableExtra(Intent.ExtraStream, Java.Lang.Class.FromType(typeof(Uri))) as Uri;
59 }
60 else {
61#pragma warning disable CA1422
62 uri = intent.GetParcelableExtra(Intent.ExtraStream) as Uri;
63#pragma warning restore CA1422
64 }
65 }
66 if (uri == null) {
67 Toast.MakeText(this, Resources?.GetString(Resource.String.FileNotFound), ToastLength.Short)?.Show();
68 FinishAndRemoveTask();
69 return;
70 }
71 Stream fileStream = GetStreamAndInfosFromUri(uri, out string fileName, out long fileSize);
72 if (fileSize == 0
73 || fileStream == null) {
74 Toast.MakeText(this, Resources?.GetString(Resource.String.FileNotFound), ToastLength.Short)?.Show();
75 FinishAndRemoveTask();
76 return;
77 }
78 string extension = Storage.GetExtension(fileName)?.ToLowerInvariant();
82 permissionGranted = true;
83 }
84 else {
85 while (true) {
86 Thread.Sleep(100);
88 break;
89 }
92 break;
93 }
94 }
95 }
96 new AlertDialog.Builder(this).SetTitle(Resources?.GetString(Resource.String.Import))
97 ?.SetMessage(string.Format(Resources?.GetString(Resource.String.InsureImporting)!, fileName))
98 ?.SetPositiveButton(
99 Resources?.GetString(Resource.String.Yes)!,
100 async void (_, _) => await ImportFileAsync(fileName, fileSize, fileStream)
101 )
102 ?.SetNegativeButton(Resources?.GetString(Resource.String.No)!, (_, _) => FinishAndRemoveTask())
103 ?.Show();
104 }
105 else {
106 new AlertDialog.Builder(this).SetTitle(Resources?.GetString(Resource.String.NotSupportedType))
107 ?.SetMessage(string.Format(Resources?.GetString(Resource.String.FileTypeIsNotSupported)!, extension))
108 ?.SetPositiveButton(Resources?.GetString(Resource.String.Ok)!, (_, _) => FinishAndRemoveTask())
109 ?.SetOnCancelListener(new DialogInterfaceOnCancelListener(FinishAndRemoveTask))
110 ?.Show();
111 }
112 }
113
114 public async Task ImportFileAsync(string name, long size, Stream stream) {
115 AlertDialog importingDialog = null;
116 try {
118 if (size > 10L * 1024 * 1024) {
119 RunOnUiThread(() => {
120 LinearLayout layout = new(this) { Orientation = Orientation.Vertical };
121 int margin = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 24, Resources?.DisplayMetrics);
122 layout.SetPadding(0, margin, 0, margin);
123 layout.SetGravity(GravityFlags.Center);
124 ProgressBar progressBar = new(this) { Indeterminate = true };
125 layout.AddView(progressBar);
126 importingDialog = new AlertDialog.Builder(this).SetTitle(Resources?.GetString(Resource.String.Importing))
127 ?.SetView(layout)
128 ?.SetCancelable(false)
129 ?.Show();
130 }
131 );
132 }
133 await Task.Run(() => {
134 switch (type) {
135 case ExternalContentType.World: WorldsManager.ImportWorld(stream); break;
136 case ExternalContentType.BlocksTexture: BlocksTexturesManager.ImportBlocksTexture(name, stream); break;
137 case ExternalContentType.CharacterSkin: CharacterSkinsManager.ImportCharacterSkin(name, stream); break;
138 case ExternalContentType.FurniturePack: FurniturePacksManager.ImportFurniturePack(name, stream); break;
139 case ExternalContentType.Mod: ModsManager.ImportMod(name, stream ); break;
140 }
141 }
142 );
143 stream.Close();
144 await stream.DisposeAsync();
145 RunOnUiThread(() => {
146 importingDialog?.Dismiss();
147 AlertDialog.Builder builder = new AlertDialog.Builder(this)
148 .SetTitle(Resources?.GetString(Resource.String.ImportedSuccessfully))
149 ?.SetPositiveButton(
150 Resources?.GetString(Resource.String.Yes)!,
151 (_, _) => {
152 Intent intent = new(this, typeof(MainActivity));
153 intent.SetFlags(ActivityFlags.ReorderToFront);
154 StartActivity(intent);
155 FinishAndRemoveTask();
156 }
157 )
158 ?.SetNegativeButton(Resources?.GetString(Resource.String.No)!, (_, _) => FinishAndRemoveTask());
159 builder?.SetMessage(Resources?.GetString(Resource.String.InsureLaunchingGame));
160 builder?.Show();
161 }
162 );
163 }
164 catch (Exception e) {
165 RunOnUiThread(() => {
166 importingDialog?.Dismiss();
167 new AlertDialog.Builder(this).SetTitle(Resources?.GetString(Resource.String.FailedToImport))
168 ?.SetMessage(e.Message)
169 ?.SetPositiveButton(Resources?.GetString(Resource.String.Ok)!, (_, _) => FinishAndRemoveTask())
170 ?.SetOnCancelListener(new DialogInterfaceOnCancelListener(FinishAndRemoveTask))
171 ?.Show();
172 }
173 );
174 }
175 }
176
177 public Stream GetStreamAndInfosFromUri(Uri uri, out string name, out long size) {
178 name = null;
179 size = 0L;
180 Stream stream = null;
181 try {
182 using (ICursor cursor = ContentResolver?.Query(uri, null, null, null, null)) {
183 if (cursor != null
184 && cursor.MoveToFirst()) {
185 int nameIndex = cursor.GetColumnIndex(IOpenableColumns.DisplayName);
186 if (nameIndex >= 0) {
187 name = cursor.GetString(nameIndex);
188 }
189 int sizeIndex = cursor.GetColumnIndex(IOpenableColumns.Size);
190 if (sizeIndex >= 0) {
191 size = cursor.GetLong(sizeIndex);
192 }
193 }
194 }
195 stream = ContentResolver?.OpenInputStream(uri);
196 }
197 catch {
198 // ignored
199 }
200 if (string.IsNullOrEmpty(name)) {
201 name = Path.GetFileName(uri.Path);
202 }
203 return stream;
204 }
205
206 protected override void OnResume() {
207 base.OnResume();
208 if (!permissionGranted) {
210 }
211 }
212
213 public class DialogInterfaceOnCancelListener : Java.Lang.Object, IDialogInterfaceOnCancelListener {
214 readonly Action _onCancel;
215 public DialogInterfaceOnCancelListener(Action onCancel) => _onCancel = onCancel;
216 public void OnCancel(IDialogInterface dialog) => _onCancel?.Invoke();
217 }
218 }
219}
_Microsoft.Android.Resource.Designer.Resource Resource
Android.Net.Uri Uri
static string GetExtension(string path)
static string ImportBlocksTexture(string name, Stream stream)
static string ImportCharacterSkin(string name, Stream stream)
static bool IsEntryTypeDownloadSupported(ExternalContentType type)
static ExternalContentType ExtensionToType(string extension)
static string ImportFurniturePack(string name, Stream stream)
static string ImportWorld(Stream sourceStream)
static string ImportMod(string name, Stream stream)
Stream GetStreamAndInfosFromUri(Uri uri, out string name, out long size)
override void OnCreate(Bundle savedInstanceState)
async Task ImportFileAsync(string name, long size, Stream stream)
static bool CheckAndRequestPermission(Activity activity)
static bool IsPermissionGranted(Activity activity)