Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AndroidParadoxActivity.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 
4 #if SILICONSTUDIO_PLATFORM_ANDROID
5 using System;
6 using Android.App;
7 using Android.Graphics;
8 using Android.OS;
9 using Android.Views;
10 using Android.Widget;
11 using Android.Content;
12 using Android.Media;
13 using OpenTK.Platform.Android;
14 using SiliconStudio.Core;
15 using SiliconStudio.Paradox.Games;
16 using SiliconStudio.Paradox.Games.Android;
17 using SiliconStudio.Paradox.UI;
18 
19 namespace SiliconStudio.Paradox.Starter
20 {
21  public class AndroidParadoxActivity : Activity, View.IOnTouchListener
22  {
23  private AndroidGameView gameView;
24 
25  /// <summary>
26  /// The game context of the game instance.
27  /// </summary>
28  protected GameContext GameContext;
29 
30  /// <summary>
31  /// The instance of the game to run.
32  /// </summary>
33  protected Game Game;
34 
35  private RelativeLayout mainLayout;
36  private RingerModeIntentReceiver ringerModeIntentReceiver;
37 
38  protected override void OnCreate(Bundle bundle)
39  {
40  base.OnCreate(bundle);
41 
42  // Set the android global context
43  if (PlatformAndroid.Context == null)
44  PlatformAndroid.Context = this;
45 
46  // Set the format of the window color buffer (avoid conversions)
47  // TODO: PDX-364: depth format is currently hard coded (need to investigate how it can be transmitted)
48  Window.SetFormat(Format.Rgba8888);
49 
50  // Remove the title bar
51  RequestWindowFeature(WindowFeatures.NoTitle);
52 
53  // Unpack the files contained in the apk
54  //await VirtualFileSystem.UnpackAPK();
55 
56  // Create the Android OpenGl view
57  gameView = new AndroidParadoxGameView(this);
58 
59  // setup the application view and paradox game context
60  SetupGameViewAndGameContext();
61 
62  // set up a listener to the android ringer mode (Normal/Silent/Vibrate)
63  ringerModeIntentReceiver = new RingerModeIntentReceiver((AudioManager)GetSystemService(AudioService));
64  RegisterReceiver(ringerModeIntentReceiver, new IntentFilter(AudioManager.RingerModeChangedAction));
65  }
66 
67  private void SetupGameViewAndGameContext()
68  {
69  // Set the main view of the Game
70  SetContentView(Resource.Layout.Game);
71  mainLayout = FindViewById<RelativeLayout>(Resource.Id.GameViewLayout);
72  mainLayout.AddView(gameView);
73 
74  // Create the Game context
75  GameContext = new GameContext(gameView, FindViewById<RelativeLayout>(Resource.Id.EditTextLayout));
76  }
77 
78  public override void SetContentView(View view)
79  {
80  gameView = view as AndroidGameView;
81  SetupGameViewAndGameContext();
82  }
83 
84  public override void SetContentView(View view, ViewGroup.LayoutParams @params)
85  {
86  gameView = view as AndroidGameView;
87  SetupGameViewAndGameContext();
88  }
89 
90  protected override void OnPause()
91  {
92  base.OnPause();
93 
94  UnregisterReceiver(ringerModeIntentReceiver);
95 
96  if (gameView != null)
97  gameView.Pause();
98  }
99 
100  protected override void OnResume()
101  {
102  base.OnResume();
103 
104  RegisterReceiver(ringerModeIntentReceiver, new IntentFilter(AudioManager.RingerModeChangedAction));
105 
106  if (gameView != null)
107  gameView.Resume();
108  }
109 
110  public bool OnTouch(View v, MotionEvent e)
111  {
112  throw new NotImplementedException();
113  }
114 
115  private class RingerModeIntentReceiver : BroadcastReceiver
116  {
117  private readonly AudioManager audioManager;
118 
119  private int muteCounter;
120 
121  public RingerModeIntentReceiver(AudioManager audioManager)
122  {
123  this.audioManager = audioManager;
124  }
125 
126  public override void OnReceive(Context context, Intent intent)
127  {
128  UpdateMusicMuteStatus();
129  }
130 
131  private void UpdateMusicMuteStatus()
132  {
133  switch (audioManager.RingerMode)
134  {
135  case RingerMode.Normal:
136  for (int i = 0; i < muteCounter; i++)
137  audioManager.SetStreamMute(Stream.Music, false);
138  muteCounter = 0;
139  break;
140  case RingerMode.Silent:
141  case RingerMode.Vibrate:
142  audioManager.SetStreamMute(Stream.Music, true);
143  ++muteCounter;
144  break;
145  default:
146  throw new ArgumentOutOfRangeException();
147  }
148  }
149  }
150  }
151 }
152 #endif