AI2 Component  (Version nb184)
FroyoUtil.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
9 import android.app.Activity;
10 import android.content.Context;
11 import android.graphics.Bitmap;
12 import android.media.AudioManager;
13 import android.net.http.SslError;
14 import android.view.Display;
15 import android.webkit.SslErrorHandler;
16 import android.webkit.WebView;
17 import android.webkit.WebViewClient;
18 
23 
28 public class FroyoUtil {
29 
30  private FroyoUtil() {
31  }
32 
41  public static int getRotation(Display display) {
42  return display.getRotation();
43  }
44 
45  // Methods for Player Component
51  public static AudioManager setAudioManager(Activity activity) {
52  return (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
53  }
54 
60  public static Object setAudioFocusChangeListener(final Player player) {
61  Object afChangeListener = (android.media.AudioManager.OnAudioFocusChangeListener) new android
62  .media.AudioManager.OnAudioFocusChangeListener() {
63  private boolean playbackFlag = false;
70  @Override
71  public void onAudioFocusChange(int focusChange) {
72  switch(focusChange){
73  case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
74  case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
75  // Focus loss transient: Pause playback
76  if (player != null && player.playerState == Player.State.PLAYING) {
77  player.pause();
78  playbackFlag = true;
79  }
80  break;
81  case AudioManager.AUDIOFOCUS_LOSS:
82  // Focus loss permanent: focus taken by other players
83  playbackFlag = false;
84  player.OtherPlayerStarted();
85  break;
86  case AudioManager.AUDIOFOCUS_GAIN:
87  // Focus gain: Resume playback
88  if (player != null && playbackFlag && player.playerState == Player.State.PAUSED_BY_EVENT) {
89  player.Start();
90  playbackFlag = false;
91  }
92  break;
93  }
94  }
95  };
96  return afChangeListener;
97  }
98 
105  public static boolean focusRequestGranted(AudioManager am, Object afChangeListener) {
106  int result = am.requestAudioFocus((AudioManager.OnAudioFocusChangeListener) afChangeListener,
107  AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
108  return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
109  }
110 
116  public static void abandonFocus(AudioManager am, Object afChangeListener) {
117  am.abandonAudioFocus((AudioManager.OnAudioFocusChangeListener) afChangeListener);
118  }
119 
128  public static WebViewClient getWebViewClient(final boolean ignoreErrors,
129  final boolean followLinks, final Form form, final Component component) {
130  return new WebViewClient() {
131  @Override
132  public boolean shouldOverrideUrlLoading(WebView view, String url) {
133  return !followLinks;
134  }
135 
136  @Override
137  public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
138  if (ignoreErrors) {
139  handler.proceed();
140  } else {
141  handler.cancel();
142  form.dispatchErrorOccurredEvent(component, "WebView",
144  }
145  }
146 
147  @Override
148  public void onPageStarted(WebView view, String url, Bitmap favicon) {
149  EventDispatcher.dispatchEvent(component, "BeforePageLoad", url);
150  }
151 
152  @Override
153  public void onPageFinished(WebView view, String url) {
154  EventDispatcher.dispatchEvent(component, "PageLoaded", url);
155  }
156 
157  @Override
158  public void onReceivedError(WebView view, final int errorCode, final String description, final String failingUrl) {
159  form.runOnUiThread(new Runnable() {
160  public void run() {
161  EventDispatcher.dispatchEvent(component, "ErrorOccurred", errorCode, description, failingUrl);
162  }
163  });
164  }
165  };
166  }
167 
168 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_WEBVIEW_SSL_ERROR
static final int ERROR_WEBVIEW_SSL_ERROR
Definition: ErrorMessages.java:196
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.Player.State.PAUSED_BY_EVENT
PAUSED_BY_EVENT
Definition: Player.java:91
com.google.appinventor.components
com.google.appinventor.components.runtime.util.FroyoUtil.focusRequestGranted
static boolean focusRequestGranted(AudioManager am, Object afChangeListener)
Definition: FroyoUtil.java:105
com.google.appinventor.components.runtime.util.FroyoUtil.setAudioFocusChangeListener
static Object setAudioFocusChangeListener(final Player player)
Definition: FroyoUtil.java:60
com.google.appinventor.components.runtime.util.FroyoUtil.getRotation
static int getRotation(Display display)
Definition: FroyoUtil.java:41
com.google.appinventor.components.runtime.util.FroyoUtil
Definition: FroyoUtil.java:28
com.google.appinventor.components.runtime.Player.State.PLAYING
PLAYING
Definition: Player.java:91
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.util.FroyoUtil.setAudioManager
static AudioManager setAudioManager(Activity activity)
Definition: FroyoUtil.java:51
com.google.appinventor.components.runtime.util.FroyoUtil.getWebViewClient
static WebViewClient getWebViewClient(final boolean ignoreErrors, final boolean followLinks, final Form form, final Component component)
Definition: FroyoUtil.java:128
com.google.appinventor.components.runtime.Player.playerState
State playerState
Definition: Player.java:90
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.Player
Definition: Player.java:84
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.runtime.Player.State
Definition: Player.java:91
com.google
com
com.google.appinventor.components.runtime.Form
Definition: Form.java:126
com.google.appinventor.components.runtime.util.FroyoUtil.abandonFocus
static void abandonFocus(AudioManager am, Object afChangeListener)
Definition: FroyoUtil.java:116
com.google.appinventor