aboutsummaryrefslogtreecommitdiff
path: root/tools/android/packaging/xbmc/src/Main.java.in
blob: ec9f5cf700fbe05d3d3a55496b28e073e94f6988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package @APP_PACKAGE@;

import @APP_PACKAGE@.channels.util.TvUtil;

import android.app.NativeActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.hardware.input.InputManager;
import android.media.AudioManager;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Choreographer;
import android.view.View;
import android.widget.RelativeLayout;

import java.util.ArrayList;

public class Main extends NativeActivity implements Choreographer.FrameCallback
{
  private static final String TAG = "@APP_NAME@";

  public static Main MainActivity = null;
  public XBMCMainView mMainView = null;

  private XBMCSettingsContentObserver mSettingsContentObserver;
  private XBMCInputDeviceListener mInputDeviceListener;
  private XBMCJsonRPC mJsonRPC = null;
  private View mDecorView = null;
  private RelativeLayout mVideoLayout = null;
  private Handler handler = new Handler();

  private class DelayedIntent
  {
    public Intent mIntent = null;
    public int mDelay = 0;

    public DelayedIntent(Intent intent, int delay) {
      mIntent = intent;
      mDelay = delay;
    }
  }
  private ArrayList<DelayedIntent> mDelayedIntents = new ArrayList<DelayedIntent>();
  private boolean mPaused = true;


  native void _onNewIntent(Intent intent);

  native void _onActivityResult(int requestCode, int resultCode, Intent resultData);

  native void _doFrame(long frameTimeNanos);

  native void _onVisibleBehindCanceled();

  private Runnable leanbackUpdateRunnable = new Runnable()
  {
    @Override
    public void run()
    {
      Log.d(TAG, "Updating recommendations");
      new Thread()
      {
        public void run()
        {
          mJsonRPC.updateLeanback(Main.this);
        }
      }.start();
      handler.postDelayed(this, XBMCProperties.getIntProperty("xbmc.leanbackrefresh", 60 * 60) * 1000);
    }
  };

  public Main()
  {
    super();
    MainActivity = this;
  }

  public Rect getDisplayRect()
  {
    Rect ret = new Rect();
    ret.top = 0;
    ret.left = 0;
    ret.right = 0;
    ret.bottom = 0;

    try
    {
      ret.right = mDecorView.getRootView().getWidth();
      ret.bottom = mDecorView.getRootView().getHeight();
    }
    catch (Exception e)
    {
    }

    return ret;
  }

  public void registerMediaButtonEventReceiver()
  {
    AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
    manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), XBMCBroadcastReceiver.class.getName()));
  }

  public void unregisterMediaButtonEventReceiver()
  {
    AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
    manager.unregisterMediaButtonEventReceiver(new ComponentName(getPackageName(), XBMCBroadcastReceiver.class.getName()));
  }

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    System.loadLibrary("@APP_NAME_LC@");

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    mSettingsContentObserver = new XBMCSettingsContentObserver(this, handler);
    getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver);

    // Delayed Intent
    if (getIntent().getData() != null)
    {
      mDelayedIntents.add(new DelayedIntent(new Intent(getIntent()), 5000));
      getIntent().setData(null);
    }

    if (getPackageManager().hasSystemFeature("android.software.leanback"))
    {
      if (Build.VERSION.SDK_INT >= VERSION_CODES.O)
        TvUtil.scheduleSyncingChannel(this);
      else
      {
        // Leanback
        mJsonRPC = new XBMCJsonRPC();
        handler.removeCallbacks(leanbackUpdateRunnable);
        handler.postDelayed(leanbackUpdateRunnable, 30 * 1000);
      }
    }

    // register the InputDeviceListener implementation
    mInputDeviceListener = new XBMCInputDeviceListener();
    InputManager manager = (InputManager) getSystemService(INPUT_SERVICE);
    manager.registerInputDeviceListener(mInputDeviceListener, handler);

    mDecorView = getWindow().getDecorView();
    mDecorView.setBackground(null);
    getWindow().takeSurface(null);
    setContentView(R.layout.activity_main);
    mVideoLayout = (RelativeLayout) findViewById(R.id.VideoLayout);

    mMainView = new XBMCMainView(this);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    mMainView.setElevation(1);  // Always on Top
    mVideoLayout.addView(mMainView, layoutParams);

    mDecorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
    {
      @Override
      public void onSystemUiVisibilityChange(int visibility)
      {
        if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0)
        {
          handler.post(new Runnable()
          {
            public void run()
            {
              if (android.os.Build.VERSION.SDK_INT >= 19)
              {
                // Immersive mode

                // Constants from API > 17
                final int API_SYSTEM_UI_FLAG_IMMERSIVE_STICKY = 0x00001000;

                mDecorView.setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_FULLSCREEN
                                | API_SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
              }
            }
          });
        }
      }
    });
  }

  @Override
  protected void onNewIntent(Intent intent)
  {
    super.onNewIntent(intent);
    // Delay until after Resume
    if (mPaused)
    {
      Log.d("Main", "onNewIntent (delayed)");
      mDelayedIntents.add(new DelayedIntent(new Intent(intent), 500));
    }
    else
    {
      Log.d("Main", "onNewIntent (immediate)");
      _onNewIntent(intent);
    }
  }

  @Override
  public void onStart()
  {
    super.onStart();
    mDecorView.setBackgroundColor(Color.TRANSPARENT);

    Choreographer.getInstance().removeFrameCallback(this);
    Choreographer.getInstance().postFrameCallback(this);
  }

  @Override
  public void onResume()
  {
    super.onResume();

    if (android.os.Build.VERSION.SDK_INT >= 19)
    {
      // Immersive mode

      // Constants from API > 17
      final int API_SYSTEM_UI_FLAG_IMMERSIVE_STICKY = 0x00001000;

      mDecorView.setSystemUiVisibility(
              View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                      | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                      | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                      | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                      | View.SYSTEM_UI_FLAG_FULLSCREEN
                      | API_SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }

    // New intent ?
    for (final DelayedIntent delayedIntent : mDelayedIntents)
    {
      handler.postDelayed(new Runnable()
      {
        @Override
        public void run()
        {
          try
          {
            _onNewIntent(delayedIntent.mIntent);
          }
          catch (UnsatisfiedLinkError e)
          {
            Log.e("Main", "Native not registered");
          }
        }
      }, delayedIntent.mDelay);
    }
    mDelayedIntents.clear();
    mPaused = false;
  }

  @Override
  public void onPause()
  {
    super.onPause();

    if (getPackageManager().hasSystemFeature("android.software.leanback") && Build.VERSION.SDK_INT >= VERSION_CODES.O)
      TvUtil.scheduleSyncingChannel(this);

    mPaused = true;
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode,
                               Intent resultData)
  {
    super.onActivityResult(requestCode, resultCode, resultData);
    _onActivityResult(requestCode, resultCode, resultData);
  }

  @Override
  public void onDestroy()
  {
    TvUtil.cancelAllScheduledJobs(this);

    // unregister the InputDeviceListener implementation
    InputManager manager = (InputManager) getSystemService(INPUT_SERVICE);
    manager.unregisterInputDeviceListener(mInputDeviceListener);

    getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
    super.onDestroy();
  }

  @Override
  public void onVisibleBehindCanceled()
  {
    _onVisibleBehindCanceled();
    super.onVisibleBehindCanceled();
  }

  @Override
  public void doFrame(long frameTimeNanos)
  {
    Choreographer.getInstance().postFrameCallback(this);
    _doFrame(frameTimeNanos);
  }

  private native void _callNative(long funcAddr, long variantAddr);

  private void runNativeOnUiThread(final long funcAddr, final long variantAddr)
  {
    runOnUiThread(new Runnable()
    {
      @Override
      public void run()
      {
        _callNative(funcAddr, variantAddr);
      }
    });
  }
}