aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/domain/ServerInfo.java
blob: 5ac4635b7b3c8e4eb314b0a1e0596d7dbcf6d834 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2010 (C) Sindre Mehus
 */
package github.daneren2005.dsub.domain;

import android.content.Context;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.Util;

/**
 * Information about the Subsonic server.
 *
 * @author Sindre Mehus
 */
public class ServerInfo implements Serializable {
	public static final int TYPE_SUBSONIC = 1;
	public static final int TYPE_MADSONIC = 2;
	public static final int TYPE_AMPACHE = 3;
	private static final Map<Integer, ServerInfo> SERVERS = new ConcurrentHashMap<>();
	
	private boolean isLicenseValid;
	private Version restVersion;
	private int type;
	
	public ServerInfo() {
		type = TYPE_SUBSONIC;
	}

	public boolean isLicenseValid() {
		return isLicenseValid;
	}

	public void setLicenseValid(boolean licenseValid) {
		isLicenseValid = licenseValid;
	}

	public Version getRestVersion() {
		return restVersion;
	}

	public void setRestVersion(Version restVersion) {
		this.restVersion = restVersion;
	}
    
	public int getRestType() {
		return type;
	}
	public void setRestType(int type) {
		this.type = type;
	}
	
//	public boolean isStockSubsonic() {
//		return type == TYPE_SUBSONIC;
//	}
//	public boolean isMadsonic() {
//		return type == TYPE_MADSONIC;
//	}
	
	@Override
	public boolean equals(Object o) {
		if(this == o) {
			return true;
		} else if(o == null || getClass() != o.getClass()) {
			return false;
		}
	    
		final ServerInfo info = (ServerInfo) o;
	    
		if(this.type != info.type) {
			return false;
		} else if(this.restVersion == null || info.restVersion == null) {
			// Should never be null unless just starting up
			return false;
		} else {
			return this.restVersion.equals(info.restVersion);
		}
	}

//	// Stub to make sure this is never used, too easy to screw up
//	private void saveServerInfo(Context context) {
//
//	}

	public void saveServerInfo(Context context, int instance) {
		ServerInfo current = SERVERS.get(instance);
		if(!this.equals(current)) {
			SERVERS.put(instance, this);
			FileUtil.serialize(context, this, getCacheName(context, instance));
		}
	}
	
//	public static ServerInfo getServerInfo(Context context) {
//		return getServerInfo(context, Util.getActiveServer(context));
//	}

	public static ServerInfo getServerInfo(Context context, int instance) {
		ServerInfo current = SERVERS.get(instance);
		if(current != null) {
			return current;
		}
		
		current = FileUtil.deserialize(context, getCacheName(context, instance), ServerInfo.class);
		if(current != null) {
			SERVERS.put(instance, current);
		}
		
		return current;
	}

	public static Version getServerVersion(Context context) {
		return getServerVersion(context, Util.getActiveServer(context));
	}
	public static Version getServerVersion(Context context, int instance) {
		ServerInfo server = getServerInfo(context, instance);
		if(server == null) {
			return null;
		}

		return server.getRestVersion();
	}

	public static boolean checkServerVersion(Context context, String requiredVersion) {
		return checkServerVersion(context, requiredVersion, Util.getActiveServer(context));
	}
	public static boolean checkServerVersion(Context context, String requiredVersion, int instance) {
		ServerInfo server = getServerInfo(context, instance);
		if(server == null) {
			return false;
		}
		
		Version version = server.getRestVersion();
		if(version == null) {
			return false;
		}
		
		Version required = new Version(requiredVersion);
		return version.compareTo(required) >= 0;
	}

//	public static int getServerType(Context context) {
//		return getServerType(context, Util.getActiveServer(context));
//	}

	public static int getServerType(Context context, int instance) {
		if(Util.isOffline(context)) {
			return 0;
		}

		ServerInfo server = getServerInfo(context, instance);
		if(server == null) {
			return 0;
		}

		return server.getRestType();
	}

	public static boolean isStockSubsonic(Context context) {
		return isStockSubsonic(context, Util.getActiveServer(context));
	}
	public static boolean isStockSubsonic(Context context, int instance) {
		return getServerType(context, instance) == TYPE_SUBSONIC;
	}

	public static boolean isMadsonic(Context context) {
		return isMadsonic(context, Util.getActiveServer(context));
	}
	public static boolean isMadsonic(Context context, int instance) {
		return getServerType(context, instance) == TYPE_MADSONIC;
	}
	public static boolean isMadsonic6(Context context) {
		return isMadsonic6(context, Util.getActiveServer(context));
	}
	public static boolean isMadsonic6(Context context, int instance) {
		return getServerType(context, instance) == TYPE_MADSONIC && checkServerVersion(context, "2.0", instance);
	}

//	public static boolean isAmpache(Context context) {
//		return isAmpache(context, Util.getActiveServer(context));
//	}

//	public static boolean isAmpache(Context context, int instance) {
//		return getServerType(context, instance) == TYPE_AMPACHE;
//	}
	
	private static String getCacheName(Context context, int instance) {
		return "server-" + Util.getRestUrl(context, null, instance, false).hashCode() + ".ser";
	}

	public static boolean hasArtistInfo(Context context) {
		if(!isMadsonic(context) && ServerInfo.checkServerVersion(context, "1.11")) {
			return true;
		} else if(isMadsonic(context)) {
			return checkServerVersion(context, "2.0");
		} else {
			return false;
		}
	}
	
	public static boolean canBookmark(Context context) {
		return checkServerVersion(context, "1.9");
	}
	public static boolean canInternetRadio(Context context) {
		return checkServerVersion(context, "1.9");
	}

	public static boolean canSavePlayQueue(Context context) {
		return ServerInfo.checkServerVersion(context, "1.12") && (!ServerInfo.isMadsonic(context) || checkServerVersion(context, "2.0"));
	}

	public static boolean canAlbumListPerFolder(Context context) {
		return ServerInfo.checkServerVersion(context, "1.11") && (!ServerInfo.isMadsonic(context) || checkServerVersion(context, "2.0")) && !Util.isTagBrowsing(context);
	}
	public static boolean hasTopSongs(Context context) {
		return ServerInfo.isMadsonic(context) || ServerInfo.checkServerVersion(context, "1.13");
	}

//	public static boolean canUseToken(Context context) {
//		return canUseToken(context, Util.getActiveServer(context));
//	}

	public static boolean canUseToken(Context context, int instance) {
		if(isStockSubsonic(context, instance) && checkServerVersion(context, "1.14", instance)) {
			return !Util.getBlockTokenUse(context, instance);
		} else {
			return false;
		}
	}
	public static boolean hasSimilarArtists(Context context) {
		return !ServerInfo.isMadsonic(context) || ServerInfo.checkServerVersion(context, "2.0");
	}
	public static boolean hasNewestPodcastEpisodes(Context context) {
		return ServerInfo.checkServerVersion(context, "1.13");
	}

	public static boolean canRescanServer(Context context) {
		return ServerInfo.isMadsonic(context) ||
			(ServerInfo.isStockSubsonic(context) && ServerInfo.checkServerVersion(context, "1.15"));
	}
}