MCPcopy Create free account
hub / github.com/cinit/TMoe / AccountController

Class AccountController

app/src/main/java/cc/ioctl/tmoe/td/AccountController.java:13–112  ·  view source on GitHub ↗

Static helper class for accessing the Telegram account status. @author cinit

Source from the content-addressed store, hash-verified

11 * @author cinit
12 */
13public class AccountController {
14 private AccountController() {
15 throw new AssertionError("no instance for you");
16 }
17
18 /**
19 * Get the user id for the current active account slot.
20 *
21 * @param slot the account slot, non-negative
22 * @return the user id, or 0 if the slot is not logged in, or exception occurs
23 * @throws IllegalArgumentException if the slot is invalid
24 */
25 public static long getUserIdForSlot(int slot) {
26 if (slot < 0 || slot > 32767) {
27 throw new IllegalArgumentException("invalid slot: " + slot);
28 }
29 Class<?> kUserConfig = ClassLocator.getUserConfigClass();
30 if (kUserConfig == null) {
31 Utils.loge("getUserIdForSlot but UserConfig.class is null");
32 return 0;
33 }
34 try {
35 Object userConfig = kUserConfig.getMethod("getInstance", int.class).invoke(null, slot);
36 return kUserConfig.getField("clientUserId").getLong(userConfig);
37 } catch (IllegalAccessException e) {
38 // should not happen
39 throw new LinkageError("unable to access UserConfig.getInstance(I).clientUserId", e);
40 } catch (NoSuchMethodException | NoSuchFieldException | InvocationTargetException e) {
41 Utils.loge(e);
42 }
43 return 0;
44 }
45
46 /**
47 * Get the current active account slot.
48 * Notice that Telegram DOES support multiple accounts and the ongoing transaction may NOT always be the active one.
49 * AVOID USE THIS METHOD OR USE WITH CAUTION.
50 *
51 * @return the current active account slot, or -1 if exception occurs
52 * @see #getUserIdForSlot(int)
53 */
54 public static int getCurrentActiveSlot() {
55 Class<?> kUserConfig = ClassLocator.getUserConfigClass();
56 if (kUserConfig == null) {
57 Utils.loge("getCurrentActiveSlot but UserConfig.class is null");
58 return -1;
59 }
60 try {
61 return kUserConfig.getDeclaredField("selectedAccount").getInt(null);
62 } catch (IllegalAccessException e) {
63 // should not happen
64 throw new LinkageError("unable to access UserConfig.selectedAccount", e);
65 } catch (NoSuchFieldException e) {
66 Utils.loge(e);
67 }
68 return -1;
69 }
70

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected