|
| 1 | +package com.atomjack.vcfp; |
| 2 | + |
| 3 | +import android.app.AlertDialog; |
| 4 | +import android.app.Dialog; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.DialogInterface; |
| 7 | +import android.content.Intent; |
| 8 | +import android.content.SharedPreferences; |
| 9 | +import android.net.ConnectivityManager; |
| 10 | +import android.net.NetworkInfo; |
| 11 | +import android.view.View; |
| 12 | +import android.widget.AdapterView; |
| 13 | +import android.widget.ListView; |
| 14 | + |
| 15 | +import com.atomjack.vcfp.model.MediaContainer; |
| 16 | +import com.atomjack.vcfp.model.PlexClient; |
| 17 | +import com.atomjack.vcfp.model.PlexServer; |
| 18 | +import com.atomjack.vcfp.net.PlexHttpClient; |
| 19 | +import com.atomjack.vcfp.net.PlexHttpMediaContainerHandler; |
| 20 | +import com.google.gson.Gson; |
| 21 | + |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
| 25 | + |
| 26 | +public class LocalScan { |
| 27 | + private Context context; |
| 28 | + private Class theClass; |
| 29 | + private Dialog searchDialog; |
| 30 | + private Dialog serverSelectDialog = null; |
| 31 | + private LocalScanHandler scanHandler; |
| 32 | + private SharedPreferences mPrefs; |
| 33 | + private Gson gson = new Gson(); |
| 34 | + private Feedback feedback; |
| 35 | + private int serversScanned = 0; |
| 36 | + private Map<String, PlexClient> m_clients = new HashMap<String, PlexClient>(); |
| 37 | + |
| 38 | + public LocalScan(Context ctx, Class cls, SharedPreferences prefs, LocalScanHandler handler) { |
| 39 | + context = ctx; |
| 40 | + theClass = cls; |
| 41 | + scanHandler = handler; |
| 42 | + mPrefs = prefs; |
| 43 | + feedback = new Feedback(mPrefs, context); |
| 44 | + } |
| 45 | + |
| 46 | + public void searchForPlexServers() { |
| 47 | + Logger.d("searchForPlexServers()"); |
| 48 | + if(!isWifiConnected()) { |
| 49 | + showNoWifiDialog(); |
| 50 | + return; |
| 51 | + } |
| 52 | + searchDialog = new Dialog(context); |
| 53 | + |
| 54 | + searchDialog.setContentView(R.layout.search_popup); |
| 55 | + searchDialog.setTitle(context.getResources().getString(R.string.searching_for_plex_servers)); |
| 56 | + |
| 57 | + searchDialog.show(); |
| 58 | + |
| 59 | + Intent mServiceIntent = new Intent(context, GDMService.class); |
| 60 | + mServiceIntent.putExtra("ORIGIN", theClass.getSimpleName()); |
| 61 | + mServiceIntent.putExtra("class", theClass); |
| 62 | + context.startService(mServiceIntent); |
| 63 | + } |
| 64 | + |
| 65 | + public void showPlexServers() { |
| 66 | + Logger.d("servers: " + VoiceControlForPlexApplication.getPlexMediaServers().size()); |
| 67 | + if(searchDialog != null) |
| 68 | + searchDialog.dismiss(); |
| 69 | + if(serverSelectDialog == null) { |
| 70 | + serverSelectDialog = new Dialog(context); |
| 71 | + } |
| 72 | + serverSelectDialog.setContentView(R.layout.server_select); |
| 73 | + serverSelectDialog.setTitle("Select a Plex Server"); |
| 74 | + serverSelectDialog.show(); |
| 75 | + |
| 76 | + final ListView serverListView = (ListView)serverSelectDialog.findViewById(R.id.serverListView); |
| 77 | + ConcurrentHashMap<String, PlexServer> servers = new ConcurrentHashMap<String, PlexServer>(VoiceControlForPlexApplication.getPlexMediaServers()); |
| 78 | + final PlexListAdapter adapter = new PlexListAdapter(context, PlexListAdapter.TYPE_SERVER); |
| 79 | + adapter.setServers(servers); |
| 80 | + serverListView.setAdapter(adapter); |
| 81 | + serverListView.setOnItemClickListener(new ListView.OnItemClickListener() { |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) { |
| 85 | + Logger.d("Clicked position %d", position); |
| 86 | + PlexServer s = (PlexServer)parentAdapter.getItemAtPosition(position); |
| 87 | + serverSelectDialog.dismiss(); |
| 88 | + scanHandler.onDeviceSelected(s); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + public void getClients() { |
| 94 | + if(!isWifiConnected()) { |
| 95 | + showNoWifiDialog(); |
| 96 | + return; |
| 97 | + } |
| 98 | + PlexServer server = gson.fromJson(mPrefs.getString("Server", ""), PlexServer.class); |
| 99 | + if(server == null || server.name.equals(context.getResources().getString(R.string.scan_all))) { |
| 100 | + scanForClients(); |
| 101 | + } else { |
| 102 | + getClients(null); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void getClients(MediaContainer mc) { |
| 107 | + PlexServer server = gson.fromJson(mPrefs.getString("Server", ""), PlexServer.class); |
| 108 | + if(mc != null) { |
| 109 | + server.machineIdentifier = mc.machineIdentifier; |
| 110 | + //saveSettings(); |
| 111 | + } |
| 112 | + if(searchDialog == null) { |
| 113 | + searchDialog = new Dialog(context); |
| 114 | + } |
| 115 | + |
| 116 | + searchDialog.setContentView(R.layout.search_popup); |
| 117 | + searchDialog.setTitle("Searching for Plex Clients"); |
| 118 | + |
| 119 | + searchDialog.show(); |
| 120 | + PlexHttpClient.get(server.getClientsURL(), null, new PlexHttpMediaContainerHandler() { |
| 121 | + @Override |
| 122 | + public void onSuccess(MediaContainer clientMC) { |
| 123 | + // Exclude non-Plex Home Theater clients (pre 1.0.7) |
| 124 | + Map<String, PlexClient> clients = new HashMap<String, PlexClient>(); |
| 125 | + for (int i = 0; i < clientMC.clients.size(); i++) { |
| 126 | + if (!VoiceControlForPlexApplication.isVersionLessThan(clientMC.clients.get(i).version, VoiceControlForPlexApplication.MINIMUM_PHT_VERSION)) { |
| 127 | + clients.put(clientMC.clients.get(i).name, clientMC.clients.get(i)); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + searchDialog.dismiss(); |
| 132 | + if (clients.size() == 0) { |
| 133 | + AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 134 | + builder.setTitle("No Plex Clients Found"); |
| 135 | + builder.setCancelable(false) |
| 136 | + .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 137 | + public void onClick(DialogInterface dialog, int id) { |
| 138 | + dialog.cancel(); |
| 139 | + } |
| 140 | + }); |
| 141 | + AlertDialog d = builder.create(); |
| 142 | + d.show(); |
| 143 | + } else { |
| 144 | + Logger.d("Clients: " + clients.size()); |
| 145 | + showPlexClients(clients); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void onFailure(Throwable error) { |
| 151 | + searchDialog.dismiss(); |
| 152 | + feedback.e(context.getResources().getString(R.string.got_error), error.getMessage()); |
| 153 | + } |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + public void scanForClients() { |
| 158 | + if(searchDialog == null) { |
| 159 | + searchDialog = new Dialog(context); |
| 160 | + } |
| 161 | + |
| 162 | + searchDialog.setContentView(R.layout.search_popup); |
| 163 | + searchDialog.setTitle("Searching for Plex Clients"); |
| 164 | + |
| 165 | + searchDialog.show(); |
| 166 | + Intent mServiceIntent = new Intent(context, GDMService.class); |
| 167 | + mServiceIntent.putExtra("ORIGIN", "ScanForClients"); |
| 168 | + mServiceIntent.putExtra("class", theClass); |
| 169 | + context.startService(mServiceIntent); |
| 170 | + } |
| 171 | + |
| 172 | + public void showPlexClients(Map<String, PlexClient> clients) { |
| 173 | + if(serverSelectDialog == null) { |
| 174 | + serverSelectDialog = new Dialog(context); |
| 175 | + } |
| 176 | + serverSelectDialog.setContentView(R.layout.server_select); |
| 177 | + serverSelectDialog.setTitle("Select a Plex Client"); |
| 178 | + serverSelectDialog.show(); |
| 179 | + |
| 180 | + final ListView serverListView = (ListView)serverSelectDialog.findViewById(R.id.serverListView); |
| 181 | + final PlexListAdapter adapter = new PlexListAdapter(context, PlexListAdapter.TYPE_CLIENT); |
| 182 | + adapter.setClients(clients); |
| 183 | + serverListView.setAdapter(adapter); |
| 184 | + serverListView.setOnItemClickListener(new ListView.OnItemClickListener() { |
| 185 | + |
| 186 | + @Override |
| 187 | + public void onItemClick(AdapterView<?> parentAdapter, View view, int position, |
| 188 | + long id) { |
| 189 | + PlexClient s = (PlexClient)parentAdapter.getItemAtPosition(position); |
| 190 | + serverSelectDialog.dismiss(); |
| 191 | + scanHandler.onDeviceSelected(s); |
| 192 | + } |
| 193 | + |
| 194 | + }); |
| 195 | + } |
| 196 | + |
| 197 | + public void scanServersForClients() { |
| 198 | + ConcurrentHashMap<String, PlexServer> servers = VoiceControlForPlexApplication.getPlexMediaServers(); |
| 199 | + Logger.d("ScanServersForClients, number of servers = " + servers.size()); |
| 200 | + serversScanned = 0; |
| 201 | + for(PlexServer thisServer : servers.values()) { |
| 202 | + Logger.d("ScanServersForClients server: %s", thisServer.name); |
| 203 | + PlexHttpClient.get(thisServer.getClientsURL(), null, new PlexHttpMediaContainerHandler() |
| 204 | + { |
| 205 | + @Override |
| 206 | + public void onSuccess(MediaContainer clientMC) |
| 207 | + { |
| 208 | + serversScanned++; |
| 209 | + // Exclude non-Plex Home Theater clients (pre 1.0.7) |
| 210 | + Logger.d("clientMC size: %d", clientMC.clients.size()); |
| 211 | + for(int i=0;i<clientMC.clients.size();i++) { |
| 212 | + if((!VoiceControlForPlexApplication.isVersionLessThan(clientMC.clients.get(i).version, VoiceControlForPlexApplication.MINIMUM_PHT_VERSION) || !clientMC.clients.get(i).product.equals("Plex Home Theater")) && !m_clients.containsKey(clientMC.clients.get(i).name)) { |
| 213 | + m_clients.put(clientMC.clients.get(i).name, clientMC.clients.get(i)); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + if(serversScanned == VoiceControlForPlexApplication.getPlexMediaServers().size()) { |
| 218 | + searchDialog.dismiss(); |
| 219 | + if(m_clients.size() == 0) { |
| 220 | + AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 221 | + builder.setTitle("No Plex Clients Found"); |
| 222 | + builder.setCancelable(false) |
| 223 | + .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() |
| 224 | + { |
| 225 | + public void onClick(DialogInterface dialog, int id) |
| 226 | + { |
| 227 | + dialog.cancel(); |
| 228 | + } |
| 229 | + }); |
| 230 | + AlertDialog d = builder.create(); |
| 231 | + d.show(); |
| 232 | + } else { |
| 233 | + Logger.d("Clients: " + m_clients.size()); |
| 234 | + showPlexClients(m_clients); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public void onFailure(Throwable error) { |
| 241 | + searchDialog.dismiss(); |
| 242 | + feedback.e(context.getResources().getString(R.string.got_error), error.getMessage()); |
| 243 | + } |
| 244 | + }); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + public boolean isWifiConnected() { |
| 249 | + ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 250 | + NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); |
| 251 | + return mWifi.isConnected(); |
| 252 | + } |
| 253 | + |
| 254 | + public void showNoWifiDialog() { |
| 255 | + AlertDialog.Builder usageDialog = new AlertDialog.Builder(context); |
| 256 | + usageDialog.setTitle(R.string.no_wifi_connection); |
| 257 | + usageDialog.setMessage(R.string.no_wifi_connection_message); |
| 258 | + usageDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 259 | + public void onClick(DialogInterface dialog, int id) { |
| 260 | + dialog.dismiss(); |
| 261 | + } |
| 262 | + }); |
| 263 | + usageDialog.show(); |
| 264 | + } |
| 265 | +} |
0 commit comments