Home | History | Annotate | Download | only in android
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.backends.android;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.content.pm.PackageManager;
     22 import android.net.Uri;
     23 
     24 import com.badlogic.gdx.Net;
     25 import com.badlogic.gdx.net.NetJavaImpl;
     26 import com.badlogic.gdx.net.NetJavaServerSocketImpl;
     27 import com.badlogic.gdx.net.NetJavaSocketImpl;
     28 import com.badlogic.gdx.net.ServerSocket;
     29 import com.badlogic.gdx.net.ServerSocketHints;
     30 import com.badlogic.gdx.net.Socket;
     31 import com.badlogic.gdx.net.SocketHints;
     32 import com.badlogic.gdx.utils.GdxRuntimeException;
     33 
     34 /** Android implementation of the {@link Net} API.
     35  * @author acoppes */
     36 public class AndroidNet implements Net {
     37 
     38 	// IMPORTANT: The Gdx.net classes are a currently duplicated for JGLFW/LWJGL + Android!
     39 	// If you make changes here, make changes in the other backend as well.
     40 	final AndroidApplicationBase app;
     41 	NetJavaImpl netJavaImpl;
     42 
     43 	public AndroidNet (AndroidApplicationBase app) {
     44 		this.app = app;
     45 		netJavaImpl = new NetJavaImpl();
     46 	}
     47 
     48 	@Override
     49 	public void sendHttpRequest (HttpRequest httpRequest, final HttpResponseListener httpResponseListener) {
     50 		netJavaImpl.sendHttpRequest(httpRequest, httpResponseListener);
     51 	}
     52 
     53 	@Override
     54 	public void cancelHttpRequest (HttpRequest httpRequest) {
     55 		netJavaImpl.cancelHttpRequest(httpRequest);
     56 	}
     57 
     58 	@Override
     59 	public ServerSocket newServerSocket (Protocol protocol, String hostname, int port, ServerSocketHints hints) {
     60 		return new NetJavaServerSocketImpl(protocol, hostname, port, hints);
     61 	}
     62 
     63 	@Override
     64 	public ServerSocket newServerSocket (Protocol protocol, int port, ServerSocketHints hints) {
     65 		return new NetJavaServerSocketImpl(protocol, port, hints);
     66 	}
     67 
     68 	@Override
     69 	public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) {
     70 		return new NetJavaSocketImpl(protocol, host, port, hints);
     71 	}
     72 
     73 	@Override
     74 	public boolean openURI (String URI) {
     75 		boolean result = false;
     76 		final Uri uri = Uri.parse(URI);
     77 		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     78 		PackageManager pm = app.getContext().getPackageManager();
     79 		if (pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
     80 			app.runOnUiThread(new Runnable() {
     81 				@Override
     82 				public void run () {
     83 					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     84 					// LiveWallpaper and Daydream applications need this flag
     85 					if (!(app.getContext() instanceof Activity))
     86 						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     87 					app.startActivity(intent);
     88 				}
     89 			});
     90 			result = true;
     91 		}
     92 		return result;
     93 	}
     94 
     95 }
     96