forked from PojavLauncherTeam/PojavLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPojavProfile.java
More file actions
75 lines (63 loc) · 2.46 KB
/
Copy pathPojavProfile.java
File metadata and controls
75 lines (63 loc) · 2.46 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
package net.kdt.pojavlaunch;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.kdt.pojavlaunch.value.MinecraftAccount;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
public class PojavProfile {
private static final String PROFILE_PREF = "pojav_profile";
private static final String PROFILE_PREF_FILE = "file";
public static SharedPreferences getPrefs(Context ctx) {
return ctx.getSharedPreferences(PROFILE_PREF, Context.MODE_PRIVATE);
}
public static MinecraftAccount getCurrentProfileContent(@NonNull Context ctx, @Nullable String profileName) {
return MinecraftAccount.load(profileName == null ? getCurrentProfileName(ctx) : profileName);
}
public static String getCurrentProfileName(Context ctx) {
String name = getPrefs(ctx).getString(PROFILE_PREF_FILE, "");
// A dirty fix
if (!name.isEmpty() && name.startsWith(Tools.DIR_ACCOUNT_NEW) && name.endsWith(".json")) {
name = name.substring(0, name.length() - 5).replace(Tools.DIR_ACCOUNT_NEW, "").replace(".json", "");
setCurrentProfile(ctx, name);
}
return name;
}
public static List<MinecraftAccount> getAllProfiles(){
List<MinecraftAccount> mcAccountList = new ArrayList<>();;
for (String accountName : getAllProfilesList()){
mcAccountList.add(MinecraftAccount.load(accountName));
}
return mcAccountList;
}
public static List<String> getAllProfilesList(){
List<String> accountList = new ArrayList<>();
File accountFolder = new File(Tools.DIR_ACCOUNT_NEW);
if(accountFolder.exists() && accountFolder.list() != null){
for (String fileName : Objects.requireNonNull(accountFolder.list())) {
accountList.add(fileName.substring(0, fileName.length() - 5));
}
}
return accountList;
}
public static void setCurrentProfile(@NonNull Context ctx, @Nullable Object obj) {
SharedPreferences.Editor pref = getPrefs(ctx).edit();
try { if (obj instanceof String) {
String acc = (String) obj;
pref.putString(PROFILE_PREF_FILE, acc);
//MinecraftAccount.clearTempAccount();
} else if (obj == null) {
pref.putString(PROFILE_PREF_FILE, "");
} else {
throw new IllegalArgumentException("Profile must be String.class or null");
}
} finally {
pref.apply();
}
}
}