Skip to content

Commit

Permalink
Merge pull request #59 from moly/master
Browse files Browse the repository at this point in the history
Added support for opening URLs.
  • Loading branch information
badlogic committed Oct 12, 2012
2 parents d59fead + 4b92bfa commit 5ef9a9e
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 3 deletions.
Expand Up @@ -101,7 +101,7 @@ public void initialize (ApplicationListener listener, AndroidApplicationConfigur
input = new AndroidInput(this, graphics.view, config);
audio = new AndroidAudio(this, config);
files = new AndroidFiles(this.getAssets(), this.getFilesDir().getAbsolutePath());
net = new AndroidNet();
net = new AndroidNet(this);
this.listener = listener;
this.handler = new Handler();

Expand Down Expand Up @@ -173,6 +173,7 @@ public View initializeForView (ApplicationListener listener, AndroidApplicationC
input = new AndroidInput(this, graphics.view, config);
audio = new AndroidAudio(this, config);
files = new AndroidFiles(this.getAssets(), this.getFilesDir().getAbsolutePath());
net = new AndroidNet(this);
this.listener = listener;
this.handler = new Handler();

Expand All @@ -181,6 +182,7 @@ public View initializeForView (ApplicationListener listener, AndroidApplicationC
Gdx.audio = this.getAudio();
Gdx.files = this.getFiles();
Gdx.graphics = this.getGraphics();
Gdx.net = this.getNet();

createWakeLock(config);
return graphics.getView();
Expand Down Expand Up @@ -221,6 +223,7 @@ protected void onResume () {
Gdx.audio = this.getAudio();
Gdx.files = this.getFiles();
Gdx.graphics = this.getGraphics();
Gdx.net = this.getNet();

((AndroidInput)getInput()).registerSensorListeners();

Expand Down
Expand Up @@ -16,6 +16,9 @@

package com.badlogic.gdx.backends.android;

import android.content.Intent;
import android.net.Uri;

import com.badlogic.gdx.Net;
import com.badlogic.gdx.Net.HttpResult;
import com.badlogic.gdx.Net.Protocol;
Expand All @@ -28,7 +31,12 @@ public class AndroidNet implements Net {

// IMPORTANT: The Gdx.net classes are a currently duplicated for LWJGL + Android!
// If you make changes here, make changes in the other backend as well.

final AndroidApplication app;

public AndroidNet(AndroidApplication activity) {
app = activity;
}

@Override
public HttpResult httpGet (String url, String... parameters) {
throw new UnsupportedOperationException("Not implemented");
Expand All @@ -48,4 +56,15 @@ public ServerSocket newServerSocket (Protocol protocol, int port, ServerSocketHi
public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) {
return new AndroidSocket(protocol, host, port, hints);
}

@Override
public void openURI(String URI) {
final Uri uri = Uri.parse(URI);
app.runOnUiThread(new Runnable(){
@Override
public void run () {
app.startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
});
}
}
Expand Up @@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
******************************************************************************/
package com.badlogic.gdx.backends.ios;

import cli.MonoTouch.Foundation.NSDictionary;
Expand Down Expand Up @@ -100,11 +100,13 @@ public boolean ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation or
this.graphics = new IOSGraphics(getBounds(uiViewController), this, input);
this.files = new IOSFiles();
this.audio = new IOSAudio();
this.net = new IOSNet(this);

Gdx.files = this.files;
Gdx.graphics = this.graphics;
Gdx.audio = this.audio;
Gdx.input = this.input;
Gdx.net = this.net;

this.input.setupPeripherals();

Expand Down
Expand Up @@ -15,6 +15,9 @@
******************************************************************************/
package com.badlogic.gdx.backends.ios;

import cli.MonoTouch.Foundation.NSUrl;
import cli.MonoTouch.UIKit.UIApplication;

import com.badlogic.gdx.Net;
import com.badlogic.gdx.Net.HttpResult;
import com.badlogic.gdx.Net.Protocol;
Expand All @@ -25,6 +28,12 @@

public class IOSNet implements Net {

final UIApplication uiApp;

public IOSNet(IOSApplication app) {
uiApp = app.uiApp;
}

@Override
public HttpResult httpGet (String url, String... parameters) {
throw new UnsupportedOperationException("Not implemented");
Expand All @@ -44,4 +53,9 @@ public ServerSocket newServerSocket (Protocol protocol, int port, ServerSocketHi
public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) {
return new IOSSocket(protocol, host, port, hints);
}

@Override
public void openURI(String URI) {
uiApp.OpenUrl(new NSUrl(URI));
}
}
Expand Up @@ -16,11 +16,15 @@

package com.badlogic.gdx.backends.lwjgl;

import java.awt.Desktop;
import java.net.URI;

import com.badlogic.gdx.Net;
import com.badlogic.gdx.net.ServerSocket;
import com.badlogic.gdx.net.ServerSocketHints;
import com.badlogic.gdx.net.Socket;
import com.badlogic.gdx.net.SocketHints;
import com.badlogic.gdx.utils.GdxRuntimeException;

public class LwjglNet implements Net {

Expand All @@ -46,4 +50,21 @@ public ServerSocket newServerSocket (Protocol protocol, int port, ServerSocketHi
public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) {
return new LwjglSocket(protocol, host, port, hints);
}

@Override
public void openURI(String URI) {
if (!Desktop.isDesktopSupported())
return;

Desktop desktop = Desktop.getDesktop();

if (!desktop.isSupported(Desktop.Action.BROWSE))
return;

try {
desktop.browse(new java.net.URI(URI));
} catch (Exception e) {
throw new GdxRuntimeException(e);
}
}
}
Expand Up @@ -7,6 +7,7 @@
import com.badlogic.gdx.net.ServerSocketHints;
import com.badlogic.gdx.net.Socket;
import com.badlogic.gdx.net.SocketHints;
import com.google.gwt.user.client.Window;

public class GwtNet implements Net {
@Override
Expand All @@ -28,4 +29,9 @@ public ServerSocket newServerSocket (Protocol protocol, int port, ServerSocketHi
public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) {
throw new UnsupportedOperationException("Not implemented");
}

@Override
public void openURI(String URI) {
Window.open(URI, "_blank", null);
}
}
10 changes: 10 additions & 0 deletions gdx/src/com/badlogic/gdx/Net.java
Expand Up @@ -148,4 +148,14 @@ public enum Protocol {
* @return GdxRuntimeException in case the socket couldn't be opened
*/
public Socket newClientSocket(Protocol protocol, String host, int port, SocketHints hints);

/**
* Launches the default browser to display a URI. If the default browser is not able
* to handle the specified URI, the application registered for handling URIs of the
* specified type is invoked. The application is determined from the protocol
* and path of the URI.
*
* @param URI the URI to be opened.
*/
public void openURI(String URI);
}

0 comments on commit 5ef9a9e

Please sign in to comment.