diff --git a/app/src/main/java/com/jkanetwork/st/calendariodefrases/MainActivity.java b/app/src/main/java/com/jkanetwork/st/calendariodefrases/MainActivity.java index eaeda31..0fe2091 100644 --- a/app/src/main/java/com/jkanetwork/st/calendariodefrases/MainActivity.java +++ b/app/src/main/java/com/jkanetwork/st/calendariodefrases/MainActivity.java @@ -6,31 +6,33 @@ import android.content.ClipboardManager; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; +import android.text.method.ScrollingMovementMethod; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; -import com.marcohc.robotocalendar.RobotoCalendarView.RobotoCalendarListener; -import com.marcohc.robotocalendar.RobotoCalendarView; -import java.text.DateFormatSymbols; -import java.util.Calendar; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; import com.google.android.gms.ads.AdRequest; -import com.google.android.gms.ads.AdView; +import com.google.android.gms.ads.FullScreenContentCallback; +import com.google.android.gms.ads.LoadAdError; import com.google.android.gms.ads.MobileAds; +import com.google.android.gms.ads.OnUserEarnedRewardListener; +import com.google.android.gms.ads.rewarded.RewardedAd; +import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback; import com.google.android.ump.ConsentInformation; import com.google.android.ump.ConsentRequestParameters; import com.google.android.ump.FormError; import com.google.android.ump.UserMessagingPlatform; +import com.marcohc.robotocalendar.RobotoCalendarView; +import java.text.DateFormatSymbols; +import java.util.Calendar; - -public class MainActivity extends AppCompatActivity implements RobotoCalendarListener { +public class MainActivity extends AppCompatActivity implements RobotoCalendarView.RobotoCalendarListener { private RobotoCalendarView robotoCalendarView; DatabaseHelper db; @@ -39,49 +41,26 @@ public class MainActivity extends AppCompatActivity implements RobotoCalendarLis final Config conf = new Config(this); /* Will use when move month back and forth, because library doesnt show the actual month.. */ - Integer calMonth = cal.get(Calendar.MONTH)+1; //From 1 to 12, and not from 0 to 11 + Integer calMonth = cal.get(Calendar.MONTH) + 1; //From 1 to 12, and not from 0 to 11 Integer calYear = cal.get(Calendar.YEAR); - /* Texts making all activity used, and valued in onCreate */ - TextView txtfraseTitle; - TextView txtfraseText; - TextView txtfraseAutor; - /* More objects of view */ + /* Views of the redesign */ + TextView txtDate; + TextView txtSentence; + TextView txtAutor; ImageButton btnFav; ImageButton btnShare; + TextView btnChange; - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.main, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - int id = item.getItemId(); - if (id == R.id.favorites) { - startActivity(new Intent(MainActivity.this, FavsActivity.class)); - return true; - } else if (id == R.id.options) { - startActivity(new Intent(MainActivity.this, OptsActivity.class)); - return true; - } else if (id == R.id.about) { - startActivity(new Intent(MainActivity.this, AboutOfActivity.class)); - return true; - } else if (id == R.id.exit) { - moveTaskToBack(true); /* Returns to "desktop" */ - } - return super.onOptionsItemSelected(item); - } + /* Rewarded ad to change the sentence */ + private RewardedAd rewardedAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* First of all,before loading, see if its the first time using the app */ - if (conf.getFirstTime()){ + if (conf.getFirstTime()) { new AlertDialog.Builder(MainActivity.this) .setTitle(R.string.welcome_title) .setMessage(R.string.welcome_text) @@ -96,13 +75,22 @@ public class MainActivity extends AppCompatActivity implements RobotoCalendarLis } setContentView(R.layout.activity_main); - UiUtils.applySystemBarsInsets(this); + + /* Rediseño: header propio, sin ActionBar; insets con fondo bgCard */ + if (getSupportActionBar() != null) { + getSupportActionBar().hide(); + } + UiUtils.applySystemBarsInsets(this, findViewById(R.id.main_root), + ContextCompat.getColor(this, R.color.bgCard), UiUtils.isDarkTheme(this)); + UiUtils.setupBottomNav(this, UiUtils.NavTab.INICIO); + db = new DatabaseHelper(this); - txtfraseTitle = findViewById(R.id.txt_frasetitle); - txtfraseText = findViewById(R.id.txt_frasetext); - txtfraseAutor = findViewById(R.id.txt_fraseautor); + txtDate = findViewById(R.id.txt_date); + txtSentence = findViewById(R.id.txt_sentence); + txtAutor = findViewById(R.id.txt_fraseautor); btnFav = findViewById(R.id.btnFav); btnShare = findViewById(R.id.btnShare); + btnChange = findViewById(R.id.btn_change); /* Calendar basic settings */ robotoCalendarView = (RobotoCalendarView) findViewById(R.id.calendarPicker); @@ -110,15 +98,44 @@ public class MainActivity extends AppCompatActivity implements RobotoCalendarLis robotoCalendarView.setShortWeekDays(false); robotoCalendarView.showDateTitle(true); - /* Admob ads: consentimiento UMP primero, luego init en background y banner */ + /* Admob ads: consentimiento UMP primero, luego init en background y + banner + rewarded (patrón FrasesDeAnime) */ setUpConsentAndAds(); + /* Make sentence text scrollable */ + txtSentence.setMovementMethod(new ScrollingMovementMethod()); + /* Copy sentence on tap (as before) */ + txtSentence.setOnClickListener(viewIn -> { + ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + clipboard.setPrimaryClip(ClipData.newPlainText("Sentence", + txtSentence.getText() + getString(R.string.sharefrom))); + Toast.makeText(getApplicationContext(), R.string.copied, Toast.LENGTH_LONG).show(); + }); - /* Register context menu */ - registerForContextMenu(txtfraseText); + /* Fav toggle */ + btnFav.setOnClickListener(viewIn -> { + if (db.getFav(IDFrase) == 1) { + db.delFav(IDFrase); + } else { + db.addFav(IDFrase); + } + changeFavImage(); + }); - /* Close app on back button */ + /* Share */ + btnShare.setOnClickListener(viewIn -> { + String sentence = txtSentence.getText().toString(); + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/plain"); + intent.putExtra(Intent.EXTRA_TEXT, sentence + getString(R.string.sharefrom)); + startActivity(Intent.createChooser(intent, getString(R.string.share_with))); + }); + + /* Change sentence: rewarded ad (like FrasesDeAnime) */ + btnChange.setOnClickListener(viewIn -> onChangeSentenceClick()); + + /* Close app on back button: registrado con OnBackPressedDispatcher */ getOnBackPressedDispatcher().addCallback(this, new androidx.activity.OnBackPressedCallback(true) { @Override public void handleOnBackPressed() { @@ -127,7 +144,11 @@ public class MainActivity extends AppCompatActivity implements RobotoCalendarLis }); } - /* Consentimiento UMP (obligatorio EEA) y tras él, init de AdMob + banner */ + /* ------------------------------------------------------------------ */ + /* Ads: UMP consent → init background → banner adaptativo + rewarded */ + /* ------------------------------------------------------------------ */ + + /** Consentimiento UMP (obligatorio EEA) y tras él, init + carga de anuncios */ private void setUpConsentAndAds() { ConsentRequestParameters params = new ConsentRequestParameters.Builder().build(); ConsentInformation consentInfo = UserMessagingPlatform.getConsentInformation(this); @@ -140,115 +161,160 @@ public class MainActivity extends AppCompatActivity implements RobotoCalendarLis formError -> initAdsAfterConsent()); } + /** MobileAds.initialize en background y, al completar, banner + rewarded */ private void initAdsAfterConsent() { - /* MobileAds.initialize en background: no bloquee el arranque */ - new Thread(() -> { - MobileAds.initialize(getApplicationContext()); - runOnUiThread(() -> { - AdView mAdView = findViewById(R.id.adView); - AdRequest adRequest = new AdRequest.Builder().build(); - mAdView.loadAd(adRequest); - }); - }).start(); + new Thread(() -> MobileAds.initialize(getApplicationContext(), initializationStatus -> + runOnUiThread(() -> { + UiUtils.loadBannerAd(MainActivity.this, + (android.widget.FrameLayout) findViewById(R.id.adContainer)); + loadRewardedAd(); + }))).start(); } - @Override - public void onResume(){ - super.onResume(); - /* Refresh calendar */ - cal = Calendar.getInstance(); - calMonth = cal.get(Calendar.MONTH)+1; //From 1 to 12, and not from 0 to 11 - calYear = cal.get(Calendar.YEAR); + /** Carga el anuncio recompensado con el ID de UNIDAD, no el de app */ + private void loadRewardedAd() { + AdRequest adRequest = new AdRequest.Builder().build(); + RewardedAd.load(this, getString(R.string.rewarded_ad_unit_id), adRequest, + new RewardedAdLoadCallback() { + @Override + public void onAdFailedToLoad(LoadAdError adError) { + rewardedAd = null; + } + @Override + public void onAdLoaded(RewardedAd ad) { + rewardedAd = ad; + } + }); + } - //Load today's sentence and set texts - IDFrase = db.getIDFraseHoy(); + /* ------------------------------------------------------------------ */ + /* Cambiar frase con anuncio recompensado (patrón FrasesDeAnime) */ + /* ------------------------------------------------------------------ */ - /* Refresh calendar, after load sentences for checks */ - robotoCalendarView.updateView(); - markDates(); - - txtfraseText.setText(db.getFraseFromID(IDFrase)); - txtfraseAutor.setText(db.getAutorFromID(IDFrase)); - - - // ListView Item Click Listener - txtfraseText.setOnClickListener(new View.OnClickListener() { + /** + * Botón CAMBIAR FRASE: sólo tiene sentido para un día ya desbloqueado + * (hoy o un pasado accesible). Muestra el rewarded y, con la recompensa, + * diálogo para confirmar el cambio. + */ + private void onChangeSentenceClick() { + if (IDFrase == null || IDFrase <= 0) { + new AlertDialog.Builder(this) + .setMessage(R.string.rewarded_no_sentence) + .setPositiveButton(android.R.string.ok, null) + .show(); + return; + } + if (rewardedAd == null) { + new AlertDialog.Builder(this) + .setMessage(R.string.rewarded_not_ready) + .setPositiveButton(android.R.string.ok, null) + .show(); + return; + } + RewardedAd ad = rewardedAd; + rewardedAd = null; + /* Recargar tras cerrar: los RewardedAd son de un solo uso */ + ad.setFullScreenContentCallback(new FullScreenContentCallback() { @Override - public void onClick(View viewIn) { - ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); - clipboard.setPrimaryClip(ClipData.newPlainText("Sentence", txtfraseText.getText()+MainActivity.this.getString(R.string.sharefrom))); - Toast.makeText(getApplicationContext(), R.string.copied, Toast.LENGTH_LONG ).show(); + public void onAdDismissedFullScreenContent() { + loadRewardedAd(); } }); - changeFavImage(); //Fav image button + ad.show(this, rewardItem -> { + /* ANUNCIO VISTO: ofrecer el cambio de frase */ + new AlertDialog.Builder(MainActivity.this) + .setTitle(R.string.rewarded_change_title) + .setMessage(R.string.rewarded_change_msg) + .setPositiveButton(R.string.rewarded_change_ok, (d, w) -> { + db.changeDaySentence(selYear, selMonth, selDay); + IDFrase = db.getIDFraseDay(selYear, selMonth, selDay); + updateSentenceViews(); + markDates(); + }) + .setNegativeButton(android.R.string.cancel, null) + .show(); + }); + } - // SetFavListener - btnFav.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View viewIn) { - if (db.getFav(IDFrase) == 1){ - db.delFav(IDFrase); - }else { - db.addFav(IDFrase); - } - changeFavImage(); - } - }); - // ShareListener - btnShare.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View viewIn) { - String sentence = txtfraseText.getText().toString(); - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - intent.putExtra(Intent.EXTRA_TEXT, sentence+MainActivity.this.getString(R.string.sharefrom)); - startActivity(Intent.createChooser(intent, getString(R.string.share_with))); - } - }); + /* ------------------------------------------------------------------ */ + /* Sentence views */ + /* ------------------------------------------------------------------ */ + + /** Updates the pill date, sentence and author for the selected day */ + private void updateSentenceViews() { + txtSentence.setText(db.getFraseFromID(IDFrase)); + txtAutor.setText(db.getAutorFromID(IDFrase)); + Calendar today = Calendar.getInstance(); + if (selYear != today.get(Calendar.YEAR) + || selMonth != today.get(Calendar.MONTH) + 1 + || selDay != today.get(Calendar.DAY_OF_MONTH)) { + txtDate.setText(getString(R.string.sentence_of_day, selDay, getMonth(selMonth))); + } else { + txtDate.setText(R.string.txt_todaysentence); + } + changeFavImage(); } private void changeFavImage() { - if (db.getFav(IDFrase) == 1){ + if (db.getFav(IDFrase) == 1) { btnFav.setBackgroundResource(R.drawable.star_on); - }else { + } else { btnFav.setBackgroundResource(R.drawable.star_off); } } + @Override + protected void onResume() { + super.onResume(); + /* Refresh calendar */ + cal = Calendar.getInstance(); + calMonth = cal.get(Calendar.MONTH) + 1; //From 1 to 12, and not from 0 to 11 + calYear = cal.get(Calendar.YEAR); - /* Close app on back button: registrado en onCreate con OnBackPressedDispatcher */ + /* Selected day = today on resume */ + selYear = cal.get(Calendar.YEAR); + selMonth = cal.get(Calendar.MONTH) + 1; + selDay = cal.get(Calendar.DAY_OF_MONTH); + /* Load today's sentence and set texts */ + IDFrase = db.getIDFraseHoy(); + updateSentenceViews(); + /* Refresh calendar after load sentences for checks */ + robotoCalendarView.updateView(); + markDates(); + } + + /* ------------------------------------------------------------------ */ + /* Calendar listener */ + /* ------------------------------------------------------------------ */ @Override public void onDayClick(Calendar daySelectedCalendar) { - Integer month = daySelectedCalendar.get(Calendar.MONTH); - Integer day = daySelectedCalendar.get(Calendar.DAY_OF_MONTH); - Integer year = daySelectedCalendar.get(Calendar.YEAR); - month=month+1; //Now months start with 1 and not 0 - /* Load sentence */ + int year = daySelectedCalendar.get(Calendar.YEAR); + int month = daySelectedCalendar.get(Calendar.MONTH) + 1; + int day = daySelectedCalendar.get(Calendar.DAY_OF_MONTH); + selYear = year; + selMonth = month; + selDay = day; IDFrase = db.getIDFraseDay(year, month, day); - String frase = db.getFraseFromID(IDFrase); - txtfraseText.setText(frase); //Change text - txtfraseAutor.setText(db.getAutorFromID(IDFrase)); - /* Set Title before sentence */ - if (year != cal.get(Calendar.YEAR) || month != (cal.get(Calendar.MONTH)+1) || day != cal.get(Calendar.DAY_OF_MONTH)) { - txtfraseTitle.setText(getString(R.string.sentence_of_day, day, getMonth(month))); //Change title - }else { - txtfraseTitle.setText(R.string.txt_todaysentence); //Change title (today) - } - changeFavImage(); //For checking fav image + updateSentenceViews(); } + @Override public void onDayLongClick(Calendar daySelectedCalendar) { onDayClick(daySelectedCalendar); } + /* Selected day shown in the sentence card (today on resume) */ + int selYear; + int selMonth; + int selDay; /** It returns the month name */ public String getMonth(int month) { - String str = new DateFormatSymbols().getMonths()[month-1]; + String str = new DateFormatSymbols().getMonths()[month - 1]; return str.substring(0, 1).toUpperCase() + str.substring(1); } @@ -258,25 +324,24 @@ public class MainActivity extends AppCompatActivity implements RobotoCalendarLis Calendar time = Calendar.getInstance(); String[] IDDays = db.getAllDaysYet(); if (IDDays.length != 0) { - for (int x = 0; x <= IDDays.length - 1; x++) { - Integer year = Integer.parseInt(IDDays[x].substring(0, 4)); - Integer month = Integer.parseInt(IDDays[x].substring(4, 6)); - Integer day = Integer.parseInt(IDDays[x].substring(6, 8)); + for (String idDay : IDDays) { + Integer year = Integer.parseInt(idDay.substring(0, 4)); + Integer month = Integer.parseInt(idDay.substring(4, 6)); + Integer day = Integer.parseInt(idDay.substring(6, 8)); if (year.equals(calYear) && month.equals(calMonth)) { time.set(Calendar.DAY_OF_MONTH, day); - time.set(Calendar.MONTH, month-1); + time.set(Calendar.MONTH, month - 1); robotoCalendarView.markCircleImage1(time); } } } } - /* Overrides for month plus and minus */ @Override public void onRightButtonClick() { calMonth += 1; - if (calMonth > 12){ + if (calMonth > 12) { calMonth = 1; calYear += 1; } @@ -286,11 +351,10 @@ public class MainActivity extends AppCompatActivity implements RobotoCalendarLis @Override public void onLeftButtonClick() { calMonth -= 1; - if (calMonth < 1){ + if (calMonth < 1) { calMonth = 12; calYear -= 1; } markDates(); } - -} \ No newline at end of file +}