-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainActivity.cs
166 lines (131 loc) · 5.73 KB
/
MainActivity.cs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using SMAPIStardewValley;
using StardewModdingAPI;
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using Xamarin.Android.Tools.DecompressAssemblies;
using Android.Text.Method;
using Google.Android.Material.BottomNavigation;
using AndroidX.AppCompat.App;
using Android.Views;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using AlertDialog = AndroidX.AppCompat.App.AlertDialog;
namespace SMAPI_Installation
{
[Activity(Label = "SMAPIStardew Valley ", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
private const int RequestCodeStoragePermission = 1;
public static MainActivity mainActivity;
protected override void OnCreate(Bundle savedInstanceState)
{
Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
RequestWindowFeature(WindowFeatures.NoTitle);
// 确保透明状态栏
Window.AddFlags(WindowManagerFlags.KeepScreenOn);
Window.SetFlags(WindowManagerFlags.TranslucentStatus, WindowManagerFlags.TranslucentStatus);
base.OnCreate(savedInstanceState);
mainActivity = this;
ViewGroup root = FindViewById<ViewGroup>(Android.Resource.Id.Content); // 获取根视图
SetContentView(Resource.Layout.activity_main); // 设置内容视图
Window.SetSoftInputMode(SoftInput.AdjustPan);
RequestStoragePermissions();
var bottomNavigationView = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);
bottomNavigationView.ItemSelected += OnNavigationItemSelected; // 修改为 ItemSelected
bottomNavigationView.SelectedItemId = Resource.Id.navigation_game; // 设置默认选中项为“启动游戏”
bottomNavigationView.Visibility = ViewStates.Visible; // Ensure it's always visible
// 默认加载首页
LoadFragment(new GameFragment());
}
private void OnNavigationItemSelected(object sender, BottomNavigationView.ItemSelectedEventArgs e)
{
AndroidX.Fragment.App.Fragment selectedFragment = e.Item.ItemId switch
{
Resource.Id.navigation_setting => new SettingFragment(),
Resource.Id.navigation_game => new GameFragment(),
Resource.Id.navigation_mod => new ModFragment(),
_ => null
};
LoadFragment(selectedFragment);
}
private void LoadFragment(AndroidX.Fragment.App.Fragment fragment)
{
var transaction = SupportFragmentManager.BeginTransaction();
transaction.Replace(Resource.Id.fragment_container, fragment);
transaction.Commit();
}
private void RequestStoragePermissions()
{
// Handle additional functionality when storage permission is granted
Toast.MakeText(this, "存储权限已授予,继续操作", ToastLength.Short).Show();
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == RequestCodeStoragePermission)
{
if (grantResults.Length > 0 && grantResults.All(result => result == Permission.Granted))
{
// All permissions granted, continue
Toast.MakeText(this, "所有权限已授予,继续操作", ToastLength.Short).Show();
}
else
{
// Some permissions were denied
ShowErrorDialog("权限请求失败", "需要完整存储权限和应用查询权限才能继续");
}
}
}
private void ShowErrorDialog(string title, string message)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.SetTitle(title); // 设置标题
dialog.SetMessage(message); // 设置错误消息
// 添加确认按钮
dialog.SetPositiveButton("确认", (sender, e) => { });
// 显示对话框
dialog.Show();
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCodeStoragePermission)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.R)
{
if (CheckSelfPermission(Android.Manifest.Permission.ManageExternalStorage) == Permission.Granted)
{
// 权限授予,继续操作
RequestStoragePermissions();
}
else
{
// 权限未授予,提醒用户
Toast.MakeText(this, "存储权限未授予,请手动授予权限", ToastLength.Long).Show();
}
}
else
{
// 处理其他权限的结果
if (CheckSelfPermission(Android.Manifest.Permission.WriteExternalStorage) == Permission.Granted)
{
RequestStoragePermissions();
}
else
{
Toast.MakeText(this, "存储权限未授予", ToastLength.Long).Show();
}
}
}
}
// Get APK path by package name using PackageManager
// Get the application's private storage path
public static string GetPrivateStoragePath()
{
return mainActivity.GetExternalFilesDirs((string?)null)[0].ToString();
}
}
}