Browse Source

v1.1.0

master
Samuel Vermeulen 5 months ago
parent
commit
2ec076c9c9
  1. 128
      app/src/main/java/fr/svpro/radiomercure/MainActivity.java

128
app/src/main/java/fr/svpro/radiomercure/MainActivity.java

@ -2,26 +2,33 @@ package fr.svpro.radiomercure;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DownloadManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.os.IBinder;
import android.os.PowerManager;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
@ -30,27 +37,54 @@ public class MainActivity extends AppCompatActivity {
private static final int WRITE_EXTERNAL_STORAGE_RC = 100;
private WebView web;
private String url, userAgent, contentDisposition, mimeType;
private Menu menu;
private Dialog dialog;
private TextView tv_close;
private TextView tv_version;
private static String versionName;
private static final String NUMERO_TEL_RADIO = "+33375411456";
private MediaPlayerService player;
boolean serviceBound = false;
static {
versionName = BuildConfig.VERSION_NAME;
}
@SuppressLint("SetJavaScriptEnabled")
@SuppressLint({"SetJavaScriptEnabled", "WrongViewCast", "JavascriptInterface"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//-------------A revoir-----------//
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//--------------------------------//
//------Demande d'autorisation de désactiver l'optimisation de la batterie------//
AlertDialog.Builder dialogOptBattery = new AlertDialog.Builder(this);
dialogOptBattery.setMessage(R.string.dialog_battery_msg)
.setTitle(R.string.dialog_battery_titre);
dialogOptBattery.setPositiveButton(R.string.dialog_button_oui, (dialog12, id) -> {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
//intent.setData(Uri.parse("package:"+ getApplicationInfo().packageName));
startActivity(intent);
});
dialogOptBattery.setNegativeButton(R.string.dialog_button_non, (dialog1, id) -> {
// User cancelled the dialog
Toast.makeText(this, "Annulation par l'utilisateur", Toast.LENGTH_SHORT).show();
});
AlertDialog alertDialog = dialogOptBattery.create();
PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!powerManager.isIgnoringBatteryOptimizations(getApplication().getPackageName())) {
alertDialog.show();
}
}
//------------------------------------------------------------------------------------------------//
//WebBiew affichage du site
web = findViewById(R.id.wv);
web.setWebViewClient(new WebViewClient());
web.getSettings().setUserAgentString("Mozilla/5.0 "+ getApplicationInfo().packageName);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setAllowFileAccess(true);
@ -64,11 +98,12 @@ public class MainActivity extends AppCompatActivity {
web.getSettings().setUseWideViewPort(true);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//Log.w("AGENT", web.getSettings().getUserAgentString());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
web.getSettings().setAllowFileAccessFromFileURLs(true);
}
web.loadUrl("https://www.radiomercure.fr");
web.loadUrl("https://www.radiomercure.fr/?playerbar-pageinicial");
web.setDownloadListener((url, userAgent, contentDisposition, mimeType, contentLength) -> {
MainActivity.this.url = url;
@ -143,14 +178,17 @@ public class MainActivity extends AppCompatActivity {
return super.onCreateOptionsMenu(menu);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@SuppressLint("UseCompatLoadingForDrawables")
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.refresh:
web.clearCache(true);
web.reload();
Toast.makeText(this, "Rechargement...", Toast.LENGTH_SHORT).show();
reStart();
//web.reload();
//Toast.makeText(this, "Rechargement...", Toast.LENGTH_SHORT).show();
return true;
case R.id.exit:
@ -158,6 +196,7 @@ public class MainActivity extends AppCompatActivity {
return true;
case R.id.about:
dialog.show();
return true;
@ -167,6 +206,12 @@ public class MainActivity extends AppCompatActivity {
startActivity(intent);
return true;
case R.id.live:
lectureAudio("https://live.radiomercure.fr/on-air/live");
//Toast.makeText(this, "Vous écoutez Radio Mercure en direct", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
@ -182,4 +227,67 @@ public class MainActivity extends AppCompatActivity {
}
}
private void reStart() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
//Binding this Client to the AudioPlayer Service
private ServiceConnection serviceConnection = new ServiceConnection() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
MediaPlayerService.LocalBinder binder = (MediaPlayerService.LocalBinder) service;
player = binder.getService();
serviceBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
serviceBound = false;
}
};
@RequiresApi(api = Build.VERSION_CODES.O)
@SuppressLint("UseCompatLoadingForDrawables")
private void lectureAudio(String chemin) {
//Check is service is active
if (!serviceBound) {
Intent playerIntent = new Intent(this, MediaPlayerService.class);
playerIntent.putExtra("media",chemin);
startService(playerIntent);
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
Toast.makeText(this, "Lecture", Toast.LENGTH_SHORT).show();
} else {
onDestroy();
reStart();
;
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("ServiceState", serviceBound);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
serviceBound = savedInstanceState.getBoolean("ServiceState");
}
@Override
protected void onDestroy() {
super.onDestroy();
if (serviceBound) {
unbindService(serviceConnection);
//service is active
player.stopSelf();
}
}
}
Loading…
Cancel
Save