File indexing completed on 2024-05-12 15:20:44

0001 /****************************************************************************
0002 **
0003 ** SPDX-FileCopyrightText: 2013 Digia Plc and /or its subsidiary(-ies).
0004 ** Contact: http://www.qt-project.org/legal
0005 **
0006 ** SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only or GPL-3.0-only
0007 **
0008 ** This file is part of the QtAndroidExtras module of the Qt Toolkit.
0009 **
0010 ****************************************************************************/
0011 
0012 package net.gcompris;
0013 
0014 import org.qtproject.qt5.android.bindings.QtApplication;
0015 import org.qtproject.qt5.android.bindings.QtActivity;
0016 import android.media.AudioManager;
0017 import android.util.Log;
0018 import android.content.Context;
0019 import android.view.WindowManager;
0020 import java.text.Collator;
0021 import java.util.Locale;
0022 import java.util.Arrays;
0023 import java.util.List;
0024 
0025 public class GComprisActivity extends QtActivity
0026 {
0027     private static GComprisActivity m_instance;
0028     private List<Locale> availableLocales = null;
0029 
0030     public GComprisActivity()
0031     {
0032         m_instance = this;
0033     }
0034 
0035     public static boolean requestAudioFocus() {
0036     Context mContext = m_instance.getApplicationContext();
0037     AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
0038 
0039     // Request audio focus for playback
0040     int result = am.requestAudioFocus(null,
0041                       // Use the music stream.
0042                       AudioManager.STREAM_MUSIC,
0043                       // Request permanent focus.
0044                       AudioManager.AUDIOFOCUS_GAIN);
0045    
0046     if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
0047         return true;
0048     }
0049     return false;
0050     }
0051 
0052     public static void abandonAudioFocus() {
0053     // Abandon audio focus
0054     Context mContext = m_instance.getApplicationContext();
0055     AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
0056     am.abandonAudioFocus(null);
0057     }
0058 
0059     /**
0060      * Toggle activation of screen-saver
0061      *
0062      * Note that the window flags *must* be modified from the UI thread
0063      * otherwise it has no effect.
0064      *
0065      * @param value  Whether screensaver should be enabled or disabled
0066      */
0067     public void setKeepScreenOn(boolean value) {
0068         if (value)
0069             GComprisActivity.this.runOnUiThread(new Runnable() {
0070                 public void run() {
0071                     Log.d(QtApplication.QtTAG, "Disabling screensaver");
0072                     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
0073                 }
0074             });
0075         else
0076             GComprisActivity.this.runOnUiThread(new Runnable() {
0077                 public void run() {
0078                     Log.d(QtApplication.QtTAG, "Enabling screensaver");
0079                     getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
0080                 }
0081             });
0082     }
0083 
0084     public int localeCompare(String a, String b, String locale)
0085     {
0086         // Only initialize once the available locales list
0087         if(availableLocales == null)
0088             availableLocales = Arrays.asList(Collator.getAvailableLocales());
0089         String[] parts = locale.split("[\\._]");
0090         Locale l;
0091         if (parts.length >= 2 && availableLocales.contains(new Locale(parts[0], parts[1])))
0092             l = new Locale(parts[0], parts[1]);
0093         else if (availableLocales.contains(new Locale(parts[0])))
0094             l = new Locale(parts[0]);
0095         else
0096             l = Locale.getDefault();
0097         Collator collator = Collator.getInstance(l);
0098         // Note: This works only if the device supports the
0099         // passed locale. If it does not or if an invalid locale string has been
0100         // passed, the collator seems to sort according to Locale.getDefault()
0101                 return collator.compare(a, b);
0102     }
0103 }