Thiemo Melhorn

DroidScript ist eine App, wo man eigene Apps auf Basis von JavaScript am mobilen Gerät unter Android, programmieren kann.

Verbesserter Radioplayer


var layMain = null;
var isLightTheme = false;
var toggleSwitch = null;
var drawerWidth;
var layContent, txtBarTitle, lstMenu, layBar, layDrawer, layDrawerTop,
   drawerScroll;
var player, currentStationUrl = null;
var isPlayerReady = false;
var showNotification = app.LoadBoolean("notify", true);
var radioStations = [];
var dlg;

function OnStart() {
   LoadRadioStations();

   var savedTheme = app.LoadText("SavedTheme");
   if (savedTheme === "Light") {
      isLightTheme = true;
   }

   CreateMainLayout();
   ApplyTheme(isLightTheme);
}

function OnResume() {
   if (player && player.IsPlaying()) {
      player.Play();
   }
}

function OnPause() {
   if (player && player.IsPlaying()) {
      player.Play();
   }
}

function OnStop() {
   if (player) {
      player.Stop();
   }
}

function LoadRadioStations() {
   var jsonData = app.ReadFile("radio.json");
   if(jsonData) {
      radioStations = JSON.parse(jsonData);
   } else {
      app.ShowPopup("Radiosender-Datei nicht gefunden.");
   }
}

function CreateMainLayout() {
   app.SetOrientation("Portrait");

   layMain = app.CreateLayout("Linear", "FillXY");
   CreateActionBar();
   CreateDrawer();
   layContent = app.CreateLayout("Linear", "VCenter,FillXY");
   layContent.SetSize(1, 0.9);

   layMain.AddChild(layContent);
   app.AddLayout(layMain);
   app.AddDrawer(drawerScroll, "Left", drawerWidth);

   LoadStartPageContent();
   if(showNotification) CreateNotification();
}

function CreateActionBar() {
   layBar = app.AddLayout(layMain, "Card", "Horizontal,FillX");
   layBar.SetElevation(8);

   txtBarTitle = app.AddText(layBar, "Startseite", 1);
   txtBarTitle.SetPadding(16, 10, 12, 10, "dip");
   txtBarTitle.SetTextSize(22);
   txtBarTitle.SetTextColor("#ffffff");

   layBarTitle = app.AddLayout(layBar, "Linear", "Horizontal,Left");
   txtBurger = app.AddText(layBarTitle, "[fa-bars]", -1, -1,
      "FontAwesome");
   txtBurger.SetPadding(16, 12, 12, 12, "dip");
   txtBurger.SetTextSize(24);
   txtBurger.SetTextColor("#eeeeee");
   txtBurger.SetOnTouchUp(function() {
      app.OpenDrawer();
   });
}

function CreateDrawer() {
   drawerWidth = 0.75;
   drawerScroll = app.CreateScroller(drawerWidth, -1, "FillY");
   layDrawer = app.CreateLayout("Linear", "Left");
   drawerScroll.AddChild(layDrawer);

   layDrawerTop = app.CreateLayout("Absolute");
   layDrawerTop.SetSize(drawerWidth, 0.25);
   layDrawer.AddChild(layDrawerTop);

   var layMenu = app.CreateLayout("Linear", "Left");
   layDrawer.AddChild(layMenu);

   var menuItems = "Startseite::[fa-home],Einstellungen::[fa-cog]";
   lstMenu = app.AddList(layMenu, menuItems, drawerWidth, -1,
      "Menu,Expand");
   lstMenu.SetColumnWidths(-1, 0.3, 0.18);
   lstMenu.SelectItemByIndex(0, true);
   lstMenu.SetOnTouch(lstMenu_OnTouch);
}

function lstMenu_OnTouch(title, body, type, index) {
   app.CloseDrawer("Left");

   txtBarTitle.SetText(title);

   layMain.RemoveChild(layContent);
   layContent = app.CreateLayout("Linear", "VCenter,FillXY");
   layContent.SetSize(1, 0.9);
   layMain.AddChild(layContent);

   if(title == "Startseite") {
      LoadStartPageContent();
   } else if(title == "Einstellungen") {
      LoadSettingsPageContent();
   }
}

function LoadStartPageContent() {
   var savedStationName = app.LoadText("SavedStationName", "Antenne 1");
   var savedStationUrl = app.LoadText("SavedStationUrl", "");

   var stationSpinner = app.CreateSpinner(radioStations.map(function(station) {
      return station.name;
   }));

   stationSpinner.SelectItem(savedStationName);
   currentStationUrl = savedStationUrl;

   stationSpinner.SetBackColor(isLightTheme ? "#f0f0f0" : "#303030");
   stationSpinner.SetTextColor(isLightTheme ? "#000000" : "#ffffff");

   // Layout für die Buttons erstellen
   var layRow = app.CreateLayout("Linear", "Horizontal");
   
   var btnPlus = app.AddText(layRow, "[fa-plus]", 0.3, 0.1, "FontAwesome");
   btnPlus.SetOnTouch(AddNewStationForm);
   btnPlus.SetTextSize(40);
   btnPlus.SetTextColor(isLightTheme ? "#000000" : "#ffffff");

   var btnPlay = app.AddText(layRow, "[fa-play]", 0.3, 0.1, "FontAwesome");
   btnPlay.SetTextSize(40);
   btnPlay.SetTextColor(isLightTheme ? "#000000" : "#ffffff");

   var btnStop = app.AddText(layRow, "[fa-stop]", 0.3, 0.1, "FontAwesome");
   btnStop.SetTextSize(40);
   btnStop.SetTextColor(isLightTheme ? "#000000" : "#ffffff");

   player = app.CreateMediaPlayer();
   player.SetOnReady(function() {
      app.ShowPopup("Radio Player ist bereit.");
      isPlayerReady = true;
   });

   stationSpinner.SetOnTouch(function(item) {
      var selectedStation = radioStations.find(function(station) {
         return station.name === item;
      });

      if (selectedStation) {
         currentStationUrl = selectedStation.url;
         player.SetFile(currentStationUrl);

         app.SaveText("SavedStationName", selectedStation.name);
         app.SaveText("SavedStationUrl", selectedStation.url);
      }
   });

   btnPlay.SetOnTouch(function() {
      if (isPlayerReady && currentStationUrl) {
         player.Play();
         
         setTimeout(function() {
            if (!player.IsPlaying()) {
               app.ShowPopup("Fehler beim Abspielen des Radiosenders.");
            }
         }, 5000);
      } else if (!isPlayerReady) {
         app.ShowPopup("Player ist noch nicht bereit.");
      } else {
         app.ShowPopup("Bitte wähle zuerst einen Radiosender aus.");
      }
   });

   btnStop.SetOnTouch(function() {
      player.Stop();
   });

   layContent.AddChild(stationSpinner);
   layContent.AddChild(layRow); // Hinzufügen des Button-Layouts
}

function AddNewStationForm() {
    if (!dlg) { // Überprüft, ob das Dialogfenster schon existiert
        dlg = app.CreateDialog("Radiosender verwalten");

        var layDlg = app.CreateLayout("Linear", "VCenter,FillXY");
        dlg.AddLayout(layDlg);

        // Spinner zur Auswahl eines Senders zum Löschen
        var stationSpinner = app.CreateSpinner(radioStations.map(function (station) {
            return station.name;
        }), 0.8);
        layDlg.AddChild(stationSpinner);

        // Textfelder zum Hinzufügen neuer Sender
        var txtName = app.CreateTextEdit("", 0.8, 0.1);
        txtName.SetHint("Name des Radiosenders");
        layDlg.AddChild(txtName);

        var txtUrl = app.CreateTextEdit("", 0.8, 0.1);
        txtUrl.SetHint("URL des Radiosenders");
        layDlg.AddChild(txtUrl);

        // Layout für die Buttons zum Hinzufügen und Entfernen erstellen
        var layRow = app.CreateLayout("Linear", "Horizontal");

        // Button zum Hinzufügen eines neuen Senders
        var btnAddStation = app.CreateButton("+", 0.4, 0.1);
        btnAddStation.SetOnTouch(function () {
            var newStation = {
                name: txtName.GetText(),
                url: txtUrl.GetText()
            };

            if (newStation.name && newStation.url) {
                radioStations.push(newStation);
                app.WriteFile("radio.json", JSON.stringify(radioStations));
                app.ShowPopup("Radiosender hinzugefügt!");
                LoadStartPageContent(); // Aktualisiere die Liste der Sender
                dlg.Hide();
            } else {
                app.ShowPopup("Bitte Name und URL eingeben.");
            }
        });
        layRow.AddChild(btnAddStation);

        // Button zum Entfernen des ausgewählten Senders
        var btnRemoveStation = app.CreateButton("-", 0.4, 0.1);
        btnRemoveStation.SetOnTouch(function () {
            var selectedStation = stationSpinner.GetText();

            if (selectedStation) {
                var index = radioStations.findIndex(function (station) {
                    return station.name === selectedStation;
                });

                if (index > -1) {
                    radioStations.splice(index, 1); // Entferne den Sender aus dem Array
                    app.WriteFile("radio.json", JSON.stringify(radioStations)); // Datei aktualisieren
                    app.ShowPopup("Radiosender gelöscht!");
                    LoadStartPageContent(); // Aktualisiere die Liste der Sender
                    dlg.Hide();
                } else {
                    app.ShowPopup("Radiosender nicht gefunden.");
                }
            } else {
                app.ShowPopup("Bitte wählen Sie einen Sender aus.");
            }
        });
        layRow.AddChild(btnRemoveStation);

        // Füge das Layout mit den Buttons zum Hauptlayout hinzu
        layDlg.AddChild(layRow);
    }
    
    // Zeige das Dialogfenster an
    dlg.Show();
}

function CreateNotification() {
   var notify = app.CreateNotification();
   notify.SetLargeImage("Img/tmRadioPlayer.png");
   notify.SetSmallImage("Img/tmRadioPlayer.png");
   notify.SetMessage("tmRadioPlayer", "Wiedergabe: " +
      currentStationUrl);
   notify.SetOnNotify(function() {
      app.ShowPopup("Radio Player läuft im Hintergrund.");
   });
   notify.Notify("tmRadioPlayer");
}

function ClearNotification() {
   var emptyNotify = app.CreateNotification();
   emptyNotify.SetMessage("", "", "");
   emptyNotify.Notify("");
}

function LoadSettingsPageContent() {
   var txtThemeSwitch = app.AddText(layContent,
      "Thema umschalten:", 1, -1, "Center");
   txtThemeSwitch.SetTextSize(18);
   txtThemeSwitch.SetTextColor(isLightTheme ? "#000000" :
      "#ffffff");

   toggleSwitch = app.CreateSwitch("");
   toggleSwitch.SetChecked(isLightTheme);
   toggleSwitch.SetOnTouch(toggleSwitch_OnTouch);
   layContent.AddChild(txtThemeSwitch);
   layContent.AddChild(toggleSwitch);

   var txtNotifySwitch = app.AddText(layContent,
      "Benachrichtigungen aktivieren:", 1, -1, "Center");
   txtNotifySwitch.SetTextSize(18);
   txtNotifySwitch.SetTextColor(isLightTheme ? "#000000" :
      "#ffffff");

   var notifySwitch = app.CreateSwitch("");
   notifySwitch.SetChecked(showNotification);
   notifySwitch.SetOnTouch(function(isChecked) {
      showNotification = isChecked;
      app.SaveBoolean("notify", showNotification);
      if(showNotification) {
         CreateNotification();
      } else {
         ClearNotification();
      }
   });

   layContent.AddChild(txtNotifySwitch);
   layContent.AddChild(notifySwitch);
}

function ApplyTheme(isLightTheme) {
   layMain.SetBackColor(isLightTheme ? "#f0f0f0" : "#303030");
   layBar.SetBackColor(isLightTheme ? "#6200EE" : "#000000");
   layDrawerTop.SetBackColor(isLightTheme ? "#6200EE" : "#000000");
   drawerScroll.SetBackColor(isLightTheme ? "#ffffff" : "#505050");

   if(toggleSwitch) {
      toggleSwitch.SetChecked(isLightTheme);
   }
}

function toggleSwitch_OnTouch(isChecked) {
   isLightTheme = isChecked;
   app.SaveText("SavedTheme", isLightTheme ? "Light" : "Dark");
   ApplyTheme(isLightTheme);
}