WIP: migración 2024 sin commitear (AGP 7.0.4, Gradle 7.2, Kotlin 1.5.20, ViewBinding, AdMob 21) + README actualizado
Snapshot antes de migrar a toolchain moderna. Los CRLF vienen del trabajo original en Windows.
This commit is contained in:
1 parent
415a10c1af
commit
8dd05334ec
19 files changed
+2384
-2316
No files matched your search
@@ -1,40 +1,39 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.jkanetwork.st.frasesdeanime">
|
||||
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<!-- For Admob -->
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity android:name=".MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".FavsActivity" />
|
||||
<activity android:name=".AnimesSelActivity" />
|
||||
<activity android:name=".OptsActivity" />
|
||||
<activity android:name=".AboutActivity" />
|
||||
<receiver android:process=":alarm" android:name=".AlarmNotif" android:enabled="true" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED"/>
|
||||
<action android:name="com.jkanetwork.st.frasesdeanime.notif" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<meta-data
|
||||
android:name="com.google.android.gms.ads.APPLICATION_ID"
|
||||
android:value="@string/admob_id"/>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<!-- For Admob -->
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity android:name=".MainActivity" android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".FavsActivity" android:exported="false"/>
|
||||
<activity android:name=".AnimesSelActivity" android:exported="false"/>
|
||||
<activity android:name=".OptsActivity" android:exported="false"/>
|
||||
<activity android:name=".AboutActivity" android:exported="false"/>
|
||||
<receiver android:process=":alarm" android:name=".AlarmNotif" android:exported="true" android:enabled="true" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED"/>
|
||||
<action android:name="com.jkanetwork.st.frasesdeanime.notif" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<meta-data
|
||||
android:name="com.google.android.gms.ads.APPLICATION_ID"
|
||||
android:value="@string/admob_id"/>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -1,36 +1,36 @@
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.content.Context
|
||||
import android.widget.TextView
|
||||
import android.widget.ImageView
|
||||
import android.view.View
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import org.jetbrains.anko.*
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 30/08/17.
|
||||
*/
|
||||
class AboutActivity : AppCompatActivity(){
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
//Anko DSL Layout
|
||||
verticalLayout {
|
||||
textView{
|
||||
text = "¡Gracias por usar Frases de Anime! \nIdea por Alba García y desarrollada por JKANetwork!"
|
||||
textSize = 22f
|
||||
textColor = Color.BLACK
|
||||
textAlignment = View.TEXT_ALIGNMENT_CENTER
|
||||
}.lparams(width=wrapContent, height=wrapContent){padding= dip(12)}
|
||||
imageView{
|
||||
setImageResource(R.drawable.cerebro)
|
||||
}.lparams(width=matchParent, height=matchParent){}
|
||||
background = ColorDrawable(Color.parseColor("#F8F2F2"))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.content.Context
|
||||
import android.widget.TextView
|
||||
import android.widget.ImageView
|
||||
import android.view.View
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import org.jetbrains.anko.*
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 30/08/17.
|
||||
*/
|
||||
class AboutActivity : AppCompatActivity(){
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
//Anko DSL Layout
|
||||
verticalLayout {
|
||||
textView{
|
||||
text = "¡Gracias por usar Frases de Anime! \nIdea por Alba García y desarrollada por JKANetwork!"
|
||||
textSize = 22f
|
||||
textColor = Color.BLACK
|
||||
textAlignment = View.TEXT_ALIGNMENT_CENTER
|
||||
}.lparams(width=wrapContent, height=wrapContent){padding= dip(12)}
|
||||
imageView{
|
||||
setImageResource(R.drawable.cerebro)
|
||||
}.lparams(width=matchParent, height=matchParent){}
|
||||
background = ColorDrawable(Color.parseColor("#F8F2F2"))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +1,58 @@
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import java.util.Calendar
|
||||
import android.app.AlarmManager
|
||||
import androidx.core.app.NotificationCompat
|
||||
import android.app.PendingIntent
|
||||
|
||||
/*
|
||||
Help of: https://github.com/okwrtdsh/AlarmTest/blob/master/app/src/main/kotlin/com/github/okwrtdsh/alarmtest/AlarmBroadcastReceiver.kt
|
||||
|
||||
*/
|
||||
|
||||
class AlarmNotif : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent?) {
|
||||
var db = SQLiteHelper(context)
|
||||
var P = Prefs(context)
|
||||
val time = Calendar.getInstance()
|
||||
var frase = db.getSentenceFromIDF(db.getIDFraseHoy())
|
||||
var anime = db.getAnimeFromIDF(db.getIDFraseHoy())
|
||||
val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
|
||||
nm.cancel(1);
|
||||
val notificationBuilder = NotificationCompat.Builder(context, "STN")
|
||||
.setContentTitle(anime)
|
||||
.setStyle(NotificationCompat.BigTextStyle().bigText(frase))
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentText(frase)
|
||||
.setWhen(System.currentTimeMillis())
|
||||
.setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), 0))
|
||||
|
||||
var noti = notificationBuilder.build();
|
||||
|
||||
if (P.getPersNotifOn() == true) {
|
||||
noti.flags = Notification.FLAG_ONGOING_EVENT
|
||||
}
|
||||
|
||||
nm.notify(1,noti);
|
||||
|
||||
if (P.getPersNotifOn() == true) {
|
||||
/* Do notif change all days */
|
||||
time.add(Calendar.DATE, 1)
|
||||
time.set(Calendar.HOUR_OF_DAY, 0)
|
||||
time.set(Calendar.MINUTE, 0)
|
||||
time.set(Calendar.SECOND, 1)
|
||||
val sender = PendingIntent.getBroadcast(context, 1, Intent(context, AlarmNotif::class.java), 0)
|
||||
alarmMgr.setRepeating(AlarmManager.RTC, time.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sender) // Repeat every day
|
||||
}else{ /* For boot actions */
|
||||
putAlarm(context)
|
||||
}
|
||||
}
|
||||
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import java.util.Calendar
|
||||
import android.app.AlarmManager
|
||||
import androidx.core.app.NotificationCompat
|
||||
import android.app.PendingIntent
|
||||
|
||||
/*
|
||||
Help of: https://github.com/okwrtdsh/AlarmTest/blob/master/app/src/main/kotlin/com/github/okwrtdsh/alarmtest/AlarmBroadcastReceiver.kt
|
||||
|
||||
*/
|
||||
|
||||
class AlarmNotif : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent?) {
|
||||
var db = SQLiteHelper(context)
|
||||
var P = Prefs(context)
|
||||
val time = Calendar.getInstance()
|
||||
var frase = db.getSentenceFromIDF(db.getIDFraseHoy())
|
||||
var anime = db.getAnimeFromIDF(db.getIDFraseHoy())
|
||||
val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
|
||||
nm.cancel(1);
|
||||
val notificationBuilder = NotificationCompat.Builder(context, "STN")
|
||||
.setContentTitle(anime)
|
||||
.setStyle(NotificationCompat.BigTextStyle().bigText(frase))
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentText(frase)
|
||||
.setWhen(System.currentTimeMillis())
|
||||
.setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), 0))
|
||||
|
||||
var noti = notificationBuilder.build();
|
||||
|
||||
if (P.getPersNotifOn() == true) {
|
||||
noti.flags = Notification.FLAG_ONGOING_EVENT
|
||||
}
|
||||
|
||||
nm.notify(1,noti);
|
||||
|
||||
if (P.getPersNotifOn() == true) {
|
||||
/* Do notif change all days */
|
||||
time.add(Calendar.DATE, 1)
|
||||
time.set(Calendar.HOUR_OF_DAY, 0)
|
||||
time.set(Calendar.MINUTE, 0)
|
||||
time.set(Calendar.SECOND, 1)
|
||||
val sender = PendingIntent.getBroadcast(context, 1, Intent(context, AlarmNotif::class.java), 0)
|
||||
alarmMgr.setRepeating(AlarmManager.RTC, time.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sender) // Repeat every day
|
||||
}else{ /* For boot actions */
|
||||
putAlarm(context)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,101 +1,101 @@
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.view.View
|
||||
import android.widget.SimpleAdapter
|
||||
import android.widget.ListView
|
||||
import android.widget.TextView
|
||||
import android.widget.Button
|
||||
import org.jetbrains.anko.*
|
||||
import org.jetbrains.anko.support.v4.UI
|
||||
import org.jetbrains.anko.support.v4.find
|
||||
import android.widget.Toast
|
||||
import android.util.Log
|
||||
|
||||
|
||||
import androidx.core.content.ContextCompat.startActivity
|
||||
import android.content.Intent
|
||||
import android.widget.AdapterView
|
||||
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 25/07/17.
|
||||
* Help: https://www.youtube.com/watch?v=x-M1L6tcsLo
|
||||
*/
|
||||
class AnimesSelActivity : AppCompatActivity(){
|
||||
val act = this /* Activity Context */
|
||||
|
||||
/* For DSL bind */
|
||||
lateinit var view_list : ListView
|
||||
lateinit var btn_exit : Button
|
||||
lateinit var btn_sel : Button
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
var db = SQLiteHelper(applicationContext) //Database connection
|
||||
|
||||
/** Layout in DSL */
|
||||
verticalLayout {
|
||||
linearLayout {
|
||||
btn_exit = button("Volver")
|
||||
btn_sel = button("Seleccionar todos").lparams(width = matchParent)
|
||||
}
|
||||
view_list = listView {
|
||||
}.lparams(width = matchParent, height = matchParent) { }
|
||||
}
|
||||
|
||||
|
||||
if (db.getAllAnimeSel()){ //If all selected
|
||||
btn_sel.setText("Deseleccionar todos")
|
||||
}else{
|
||||
btn_sel.setText("Seleccionar todos")
|
||||
}
|
||||
|
||||
//Load ids and Names
|
||||
val arrayIDs = db.getAllAnimeIDs()
|
||||
var arrayNames = db.getAllAnimeNames()
|
||||
val length = arrayIDs.size
|
||||
|
||||
//Init List of objets
|
||||
var listSel: List<Boolean> = (emptyList())
|
||||
var listNames: List<String> = (emptyList())
|
||||
for (i in 0..length -1){ //Load selected for every one and create list names/selected
|
||||
listSel += listOf(db.getAnimeSel(arrayIDs[i]))
|
||||
listNames += listOf(arrayNames[i])
|
||||
}
|
||||
|
||||
// Load adapter for view_list with an ANKO DSL style directy, and fill with data.
|
||||
// CustomListAdapter class is in "CustomListAdapter.kt"
|
||||
view_list.adapter = CustomListAdapter<String>({ listNames }) {
|
||||
index, listNames, view ->
|
||||
linearLayout {
|
||||
checkBox {
|
||||
text = listNames[index]
|
||||
setOnClickListener { db.setAnimeSel(db.getIDAnimeFromName(listNames[index])) }
|
||||
setChecked(listSel[index])
|
||||
textSize = 18f
|
||||
|
||||
}.lparams(){topMargin = dip(6);bottomMargin = dip(6)}
|
||||
}
|
||||
}
|
||||
|
||||
btn_sel.setOnClickListener() {
|
||||
db.setAllAnimeSel()
|
||||
restartActivity(act)
|
||||
}
|
||||
|
||||
//"Return" to main
|
||||
btn_exit.setOnClickListener() {
|
||||
startActivity<MainActivity>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** On back go to main */
|
||||
override fun onBackPressed() {
|
||||
startActivity<MainActivity>()
|
||||
}
|
||||
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.view.View
|
||||
import android.widget.SimpleAdapter
|
||||
import android.widget.ListView
|
||||
import android.widget.TextView
|
||||
import android.widget.Button
|
||||
import org.jetbrains.anko.*
|
||||
import org.jetbrains.anko.support.v4.UI
|
||||
import org.jetbrains.anko.support.v4.find
|
||||
import android.widget.Toast
|
||||
import android.util.Log
|
||||
|
||||
|
||||
import androidx.core.content.ContextCompat.startActivity
|
||||
import android.content.Intent
|
||||
import android.widget.AdapterView
|
||||
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 25/07/17.
|
||||
* Help: https://www.youtube.com/watch?v=x-M1L6tcsLo
|
||||
*/
|
||||
class AnimesSelActivity : AppCompatActivity(){
|
||||
val act = this /* Activity Context */
|
||||
|
||||
/* For DSL bind */
|
||||
lateinit var view_list : ListView
|
||||
lateinit var btn_exit : Button
|
||||
lateinit var btn_sel : Button
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
var db = SQLiteHelper(applicationContext) //Database connection
|
||||
|
||||
/** Layout in DSL */
|
||||
verticalLayout {
|
||||
linearLayout {
|
||||
btn_exit = button("Volver")
|
||||
btn_sel = button("Seleccionar todos").lparams(width = matchParent)
|
||||
}
|
||||
view_list = listView {
|
||||
}.lparams(width = matchParent, height = matchParent) { }
|
||||
}
|
||||
|
||||
|
||||
if (db.getAllAnimeSel()){ //If all selected
|
||||
btn_sel.setText("Deseleccionar todos")
|
||||
}else{
|
||||
btn_sel.setText("Seleccionar todos")
|
||||
}
|
||||
|
||||
//Load ids and Names
|
||||
val arrayIDs = db.getAllAnimeIDs()
|
||||
var arrayNames = db.getAllAnimeNames()
|
||||
val length = arrayIDs.size
|
||||
|
||||
//Init List of objets
|
||||
var listSel: List<Boolean> = (emptyList())
|
||||
var listNames: List<String> = (emptyList())
|
||||
for (i in 0..length -1){ //Load selected for every one and create list names/selected
|
||||
listSel += listOf(db.getAnimeSel(arrayIDs[i]))
|
||||
listNames += listOf(arrayNames[i])
|
||||
}
|
||||
|
||||
// Load adapter for view_list with an ANKO DSL style directy, and fill with data.
|
||||
// CustomListAdapter class is in "CustomListAdapter.kt"
|
||||
view_list.adapter = CustomListAdapter<String>({ listNames }) {
|
||||
index, listNames, view ->
|
||||
linearLayout {
|
||||
checkBox {
|
||||
text = listNames[index]
|
||||
setOnClickListener { db.setAnimeSel(db.getIDAnimeFromName(listNames[index])) }
|
||||
setChecked(listSel[index])
|
||||
textSize = 18f
|
||||
|
||||
}.lparams(){topMargin = dip(6);bottomMargin = dip(6)}
|
||||
}
|
||||
}
|
||||
|
||||
btn_sel.setOnClickListener() {
|
||||
db.setAllAnimeSel()
|
||||
restartActivity(act)
|
||||
}
|
||||
|
||||
//"Return" to main
|
||||
btn_exit.setOnClickListener() {
|
||||
startActivity<MainActivity>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** On back go to main */
|
||||
override fun onBackPressed() {
|
||||
startActivity<MainActivity>()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,84 +1,84 @@
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.content.Intent
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.app.PendingIntent
|
||||
import java.util.Calendar
|
||||
import android.app.AlarmManager
|
||||
import android.util.Log
|
||||
import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 18/07/17.
|
||||
* Here, functions for using in every part of app
|
||||
*/
|
||||
|
||||
|
||||
/** Restart activity without doing animations for refresh favs
|
||||
* @param act: Use "this.intent"
|
||||
* */
|
||||
fun restartActivity(act: AppCompatActivity){
|
||||
val intent = act.intent
|
||||
act.overridePendingTransition(0, 0)
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
|
||||
act.finish()
|
||||
act.overridePendingTransition(0, 0)
|
||||
act.startActivity(intent)
|
||||
}
|
||||
|
||||
|
||||
/* Call for putting alarm */
|
||||
fun putAlarm(context: Context) {
|
||||
var P = Prefs(context)
|
||||
//val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
/* Common vars for alarm config */
|
||||
val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
val sender = PendingIntent.getBroadcast(context, 1, Intent(context, AlarmNotif::class.java), 0)
|
||||
|
||||
/* Cancel alarm first */
|
||||
alarmMgr.cancel(sender)
|
||||
//nm.cancel(1);
|
||||
|
||||
|
||||
var time = Calendar.getInstance()
|
||||
if (P.getNotifOn()) {
|
||||
if (P.getPersNotifOn()) { /* Persistent */
|
||||
var intent = Intent()
|
||||
intent.setAction("com.jkanetwork.st.frasesdeanime.notif")
|
||||
context.sendBroadcast(intent)
|
||||
|
||||
}else{ /* At x hour every day */
|
||||
|
||||
/* Here set the alarm to one time a day in selected date. Default: 8AM */
|
||||
/* Shedechule Alarm for tomorrow instead before now if alarm set to before. */
|
||||
if (time.get(Calendar.HOUR_OF_DAY) > P.getHour() || ( time.get(Calendar.HOUR_OF_DAY) == P.getHour() && time.get(Calendar.MINUTE) > P.getMinute())) {
|
||||
/* Add a day */
|
||||
time.add(Calendar.DATE, 1)
|
||||
}
|
||||
|
||||
time.set(Calendar.HOUR_OF_DAY, P.getHour())
|
||||
time.set(Calendar.MINUTE, P.getMinute())
|
||||
time.set(Calendar.SECOND, 0)
|
||||
Log.d("STLOG (Next)", time.getTime().toString())
|
||||
alarmMgr.setRepeating(AlarmManager.RTC, time.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sender) // Repeat every day
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class App : Application() {
|
||||
|
||||
companion object {
|
||||
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
}
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.content.Intent
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.app.PendingIntent
|
||||
import java.util.Calendar
|
||||
import android.app.AlarmManager
|
||||
import android.util.Log
|
||||
import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 18/07/17.
|
||||
* Here, functions for using in every part of app
|
||||
*/
|
||||
|
||||
|
||||
/** Restart activity without doing animations for refresh favs
|
||||
* @param act: Use "this.intent"
|
||||
* */
|
||||
fun restartActivity(act: AppCompatActivity){
|
||||
val intent = act.intent
|
||||
act.overridePendingTransition(0, 0)
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
|
||||
act.finish()
|
||||
act.overridePendingTransition(0, 0)
|
||||
act.startActivity(intent)
|
||||
}
|
||||
|
||||
|
||||
/* Call for putting alarm */
|
||||
fun putAlarm(context: Context) {
|
||||
var P = Prefs(context)
|
||||
//val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
/* Common vars for alarm config */
|
||||
val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
val sender = PendingIntent.getBroadcast(context, 1, Intent(context, AlarmNotif::class.java), 0)
|
||||
|
||||
/* Cancel alarm first */
|
||||
alarmMgr.cancel(sender)
|
||||
//nm.cancel(1);
|
||||
|
||||
|
||||
var time = Calendar.getInstance()
|
||||
if (P.getNotifOn()) {
|
||||
if (P.getPersNotifOn()) { /* Persistent */
|
||||
var intent = Intent()
|
||||
intent.setAction("com.jkanetwork.st.frasesdeanime.notif")
|
||||
context.sendBroadcast(intent)
|
||||
|
||||
}else{ /* At x hour every day */
|
||||
|
||||
/* Here set the alarm to one time a day in selected date. Default: 8AM */
|
||||
/* Shedechule Alarm for tomorrow instead before now if alarm set to before. */
|
||||
if (time.get(Calendar.HOUR_OF_DAY) > P.getHour() || ( time.get(Calendar.HOUR_OF_DAY) == P.getHour() && time.get(Calendar.MINUTE) > P.getMinute())) {
|
||||
/* Add a day */
|
||||
time.add(Calendar.DATE, 1)
|
||||
}
|
||||
|
||||
time.set(Calendar.HOUR_OF_DAY, P.getHour())
|
||||
time.set(Calendar.MINUTE, P.getMinute())
|
||||
time.set(Calendar.SECOND, 0)
|
||||
Log.d("STLOG (Next)", time.getTime().toString())
|
||||
alarmMgr.setRepeating(AlarmManager.RTC, time.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sender) // Repeat every day
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class App : Application() {
|
||||
|
||||
companion object {
|
||||
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
}
|
||||
}
|
||||
@@ -1,94 +1,94 @@
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.content.Intent
|
||||
import android.view.View
|
||||
import android.widget.SimpleAdapter
|
||||
import android.widget.ListView
|
||||
import android.widget.TextView
|
||||
import android.view.ContextMenu
|
||||
import android.view.MenuItem
|
||||
import android.widget.AdapterView
|
||||
import android.content.ClipboardManager
|
||||
import android.content.ClipData
|
||||
import org.jetbrains.anko.*
|
||||
|
||||
//import kotlinx.android.synthetic.main.activity_simplelistview.*
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 25/07/17.
|
||||
* Help: https://www.youtube.com/watch?v=x-M1L6tcsLo
|
||||
*/
|
||||
class FavsActivity : AppCompatActivity(){
|
||||
|
||||
/* For DSL bind */
|
||||
lateinit var view_list : ListView
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
/** Layout in DSL */
|
||||
verticalLayout {
|
||||
view_list = listView {
|
||||
}.lparams(width = matchParent, height = matchParent) {
|
||||
}
|
||||
}
|
||||
|
||||
val data = ArrayList<HashMap<String,String>>()
|
||||
var db = SQLiteHelper(applicationContext)
|
||||
val listIDFrases = db.getAllIDFavs()
|
||||
val length = listIDFrases.size
|
||||
|
||||
for (i in 0..length -1){
|
||||
val item = HashMap<String, String>()
|
||||
item.put("sentence",db.getSentenceFromIDF(listIDFrases[i]))
|
||||
item.put("anime",db.getAnimeFromIDF(listIDFrases[i]))
|
||||
data.add(item)
|
||||
}
|
||||
|
||||
view_list.adapter = SimpleAdapter(this,data,android.R.layout.simple_list_item_2,
|
||||
arrayOf("sentence","anime"), intArrayOf(android.R.id.text1,android.R.id.text2))
|
||||
|
||||
registerForContextMenu(view_list)
|
||||
|
||||
}
|
||||
|
||||
override fun onCreateContextMenu(menu: ContextMenu, v: View,
|
||||
menuInfo: ContextMenu.ContextMenuInfo) {
|
||||
super.onCreateContextMenu(menu, v, menuInfo)
|
||||
val inflater = getMenuInflater()
|
||||
inflater.inflate(R.menu.menu_favsentence, menu)
|
||||
}
|
||||
|
||||
|
||||
override fun onContextItemSelected(item: MenuItem): Boolean {
|
||||
val info = item.getMenuInfo() as AdapterView.AdapterContextMenuInfo
|
||||
val obj = view_list.getItemAtPosition(info.position) as HashMap<String, String>
|
||||
|
||||
when (item.getItemId()) {
|
||||
R.id.copy -> {
|
||||
var clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
clipboard.setPrimaryClip(ClipData.newPlainText("Sentence", "${obj.get("sentence")} (${obj.get("anime")}) \n ${this.getString(R.string.sharefrom)}"))
|
||||
toast("Copiado al portapapeles")
|
||||
return true
|
||||
}
|
||||
R.id.delfav -> {
|
||||
var db = SQLiteHelper(applicationContext)
|
||||
val IDFavs = db.getAllIDFavs()
|
||||
db.setFav(IDFavs[info.position])
|
||||
restartActivity(this)
|
||||
return true
|
||||
}
|
||||
R.id.share -> {
|
||||
share("${obj.get("sentence")} (${obj.get("anime")}) \n ${this.getString(R.string.sharefrom)}")
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.content.Intent
|
||||
import android.view.View
|
||||
import android.widget.SimpleAdapter
|
||||
import android.widget.ListView
|
||||
import android.widget.TextView
|
||||
import android.view.ContextMenu
|
||||
import android.view.MenuItem
|
||||
import android.widget.AdapterView
|
||||
import android.content.ClipboardManager
|
||||
import android.content.ClipData
|
||||
import org.jetbrains.anko.*
|
||||
|
||||
//import kotlinx.android.synthetic.main.activity_simplelistview.*
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 25/07/17.
|
||||
* Help: https://www.youtube.com/watch?v=x-M1L6tcsLo
|
||||
*/
|
||||
class FavsActivity : AppCompatActivity(){
|
||||
|
||||
/* For DSL bind */
|
||||
lateinit var view_list : ListView
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
/** Layout in DSL */
|
||||
verticalLayout {
|
||||
view_list = listView {
|
||||
}.lparams(width = matchParent, height = matchParent) {
|
||||
}
|
||||
}
|
||||
|
||||
val data = ArrayList<HashMap<String,String>>()
|
||||
var db = SQLiteHelper(applicationContext)
|
||||
val listIDFrases = db.getAllIDFavs()
|
||||
val length = listIDFrases.size
|
||||
|
||||
for (i in 0..length -1){
|
||||
val item = HashMap<String, String>()
|
||||
item.put("sentence",db.getSentenceFromIDF(listIDFrases[i]))
|
||||
item.put("anime",db.getAnimeFromIDF(listIDFrases[i]))
|
||||
data.add(item)
|
||||
}
|
||||
|
||||
view_list.adapter = SimpleAdapter(this,data,android.R.layout.simple_list_item_2,
|
||||
arrayOf("sentence","anime"), intArrayOf(android.R.id.text1,android.R.id.text2))
|
||||
|
||||
registerForContextMenu(view_list)
|
||||
|
||||
}
|
||||
|
||||
override fun onCreateContextMenu(menu: ContextMenu, v: View,
|
||||
menuInfo: ContextMenu.ContextMenuInfo) {
|
||||
super.onCreateContextMenu(menu, v, menuInfo)
|
||||
val inflater = getMenuInflater()
|
||||
inflater.inflate(R.menu.menu_favsentence, menu)
|
||||
}
|
||||
|
||||
|
||||
override fun onContextItemSelected(item: MenuItem): Boolean {
|
||||
val info = item.getMenuInfo() as AdapterView.AdapterContextMenuInfo
|
||||
val obj = view_list.getItemAtPosition(info.position) as HashMap<String, String>
|
||||
|
||||
when (item.getItemId()) {
|
||||
R.id.copy -> {
|
||||
var clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
clipboard.setPrimaryClip(ClipData.newPlainText("Sentence", "${obj.get("sentence")} (${obj.get("anime")}) \n ${this.getString(R.string.sharefrom)}"))
|
||||
toast("Copiado al portapapeles")
|
||||
return true
|
||||
}
|
||||
R.id.delfav -> {
|
||||
var db = SQLiteHelper(applicationContext)
|
||||
val IDFavs = db.getAllIDFavs()
|
||||
db.setFav(IDFavs[info.position])
|
||||
restartActivity(this)
|
||||
return true
|
||||
}
|
||||
R.id.share -> {
|
||||
share("${obj.get("sentence")} (${obj.get("anime")}) \n ${this.getString(R.string.sharefrom)}")
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,293 +1,276 @@
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import org.jetbrains.anko.*
|
||||
import android.util.Log
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.view.Menu
|
||||
import android.view.View
|
||||
import android.view.MenuItem
|
||||
import java.util.Calendar
|
||||
import java.text.DateFormatSymbols
|
||||
import androidx.viewpager.widget.ViewPager
|
||||
import android.widget.Toast
|
||||
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
|
||||
/* Ads */
|
||||
import com.google.android.gms.ads.AdRequest
|
||||
import com.google.ads.mediation.admob.AdMobAdapter
|
||||
import com.google.android.gms.ads.MobileAds
|
||||
import com.google.android.gms.ads.reward.RewardItem
|
||||
import com.google.android.gms.ads.reward.RewardedVideoAd
|
||||
import com.google.android.gms.ads.reward.RewardedVideoAdListener
|
||||
|
||||
import com.google.android.gms.ads.AdView
|
||||
import android.text.method.ScrollingMovementMethod
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity(), RewardedVideoAdListener {
|
||||
|
||||
// Overrides for making it compile ok, it needs all methods
|
||||
override fun onRewardedVideoAdLeftApplication() {}
|
||||
override fun onRewardedVideoAdClosed() {}
|
||||
override fun onRewardedVideoAdFailedToLoad(errorCode: Int) {}
|
||||
override fun onRewardedVideoAdLoaded() {}
|
||||
override fun onRewardedVideoAdOpened() {}
|
||||
override fun onRewardedVideoStarted() {}
|
||||
override fun onRewardedVideoCompleted() {}
|
||||
|
||||
/* Prefs, database init and calendar data */
|
||||
lateinit var P: Prefs
|
||||
lateinit var db: SQLiteHelper
|
||||
var cal = Calendar.getInstance()
|
||||
var dayS = cal.get(Calendar.DAY_OF_MONTH)
|
||||
var monthS = cal.get(Calendar.MONTH)+1
|
||||
var yearS = cal.get(Calendar.YEAR)
|
||||
|
||||
var IDFraseS = -2 /* Error if not changes */
|
||||
|
||||
lateinit var mRewardedVideoAd: RewardedVideoAd
|
||||
lateinit var mAdView : AdView
|
||||
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
P = Prefs(applicationContext)
|
||||
db = SQLiteHelper(applicationContext)
|
||||
|
||||
|
||||
/* Ads */
|
||||
// Initialize the Mobile Ads SDK.
|
||||
MobileAds.initialize(this, this.getString(R.string.admob_id))
|
||||
|
||||
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this)
|
||||
mRewardedVideoAd.setRewardedVideoAdListener(this);
|
||||
|
||||
val extras = Bundle()
|
||||
|
||||
extras.putString("max_ad_content_rating", "PG") //Non adult ads
|
||||
|
||||
mAdView = adView
|
||||
val adRequest = AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter::class.java, extras).build()
|
||||
mAdView.loadAd(adRequest)
|
||||
|
||||
/* Check if first time or not, for welcoming user */
|
||||
if (P.getFirstTime() == true){
|
||||
alert("Bienvenido a Frases de anime! Un lugar donde puedes descubrir las frases de tus animes favoritos, todos los días!\n" +
|
||||
"Selecciona de la siguiente lista los que más te gusten para empezar."){
|
||||
positiveButton("Empezemos") {startActivity<AnimesSelActivity>()}
|
||||
}.show()
|
||||
|
||||
}else {
|
||||
/* Check if no anime is selected */
|
||||
if (db.getCountAnimeSel() == 0) {
|
||||
alert("No tienes ningún anime seleccionado, ¿Quieres seleccionarlos ahora?", "No hay animes seleccionados") {
|
||||
positiveButton("Si") { startActivity<AnimesSelActivity>() }
|
||||
negativeButton("No") { }
|
||||
}.show()
|
||||
}
|
||||
}
|
||||
|
||||
//For "Vote us in Play Store"
|
||||
if (P.getRateCountdown() == 0){
|
||||
alert("¿Te gusta la apliación? ¿Crees que la falta algo?"){
|
||||
positiveButton("Danos tu opinión") {browse("https://play.google.com/store/apps/details?id=com.jkanetwork.st.frasesdeanime")}
|
||||
negativeButton("Más tarde") { }
|
||||
}.show()
|
||||
P.setRateCountdown(); //To -1, if not..you know
|
||||
}else{
|
||||
P.setRateCountdown(); //Countdown one
|
||||
}
|
||||
|
||||
|
||||
/* Button listeners */
|
||||
btn_fav.setOnClickListener() {
|
||||
db.setFav(IDFraseS)
|
||||
if (db.getFav(IDFraseS) == true) {
|
||||
btn_fav.setBackgroundResource(R.drawable.star_on);
|
||||
}else{
|
||||
btn_fav.setBackgroundResource(R.drawable.star_off);
|
||||
}
|
||||
}
|
||||
|
||||
btn_share.setOnClickListener() {
|
||||
share("${txt_sentence.text} (${txt_anime.text}) \n ${this.getString(R.string.sharefrom)}")
|
||||
}
|
||||
|
||||
btn_change.setOnClickListener() {
|
||||
if (mRewardedVideoAd.isLoaded() && IDFraseS > 0) {
|
||||
mRewardedVideoAd.show();
|
||||
}else if (IDFraseS <= 0) {
|
||||
alert("Aquí no puedes ver una frase aún, no sirve de nada pulsar el botón") {
|
||||
positiveButton("Ok") { }
|
||||
}.show()
|
||||
}else{
|
||||
alert("Aún no hay un anuncio disponible, espere") {
|
||||
positiveButton("Ok") { }
|
||||
}.show()
|
||||
preloadAd()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Button for previous day
|
||||
btn_prev.setOnClickListener() {
|
||||
if (dayS == 1){
|
||||
if (monthS == 1){
|
||||
monthS=12
|
||||
yearS -= 1
|
||||
}else{
|
||||
monthS -=1
|
||||
}
|
||||
if (monthS in arrayOf(1, 3, 5,7,8,10,12)){
|
||||
dayS = 31
|
||||
}else if (monthS == 2){
|
||||
dayS = 28
|
||||
}else{
|
||||
dayS = 30
|
||||
}
|
||||
}else{
|
||||
dayS -=1
|
||||
}
|
||||
changeData()
|
||||
}
|
||||
|
||||
//Button of next (day) sentence
|
||||
btn_next.setOnClickListener() {
|
||||
if ((monthS in arrayOf(1, 3, 5,7,8,10) && dayS == 31) || (monthS == 2 && dayS == 28) || (monthS in arrayOf(2,4,6,9,11) && dayS == 30)){
|
||||
monthS += 1
|
||||
dayS = 1
|
||||
}else if (dayS == 31 && monthS == 12){ //New Year
|
||||
monthS = 1
|
||||
dayS = 1
|
||||
}else{
|
||||
dayS +=1
|
||||
}
|
||||
changeData()
|
||||
}
|
||||
|
||||
btn_today.setOnClickListener(){
|
||||
//Go back to today
|
||||
dayS = cal.get(Calendar.DAY_OF_MONTH)
|
||||
monthS = cal.get(Calendar.MONTH)+1
|
||||
yearS = cal.get(Calendar.YEAR)
|
||||
changeData()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** At onResume, called after onCreate and in resume, load ad things and refresh calendar data */
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
txt_sentence.setMovementMethod(ScrollingMovementMethod()) //Make sentence text scrollable
|
||||
|
||||
preloadAd() //Load (Again) ads
|
||||
|
||||
cal = Calendar.getInstance() //Refresh calendar
|
||||
//Refresh day
|
||||
dayS = cal.get(Calendar.DAY_OF_MONTH)
|
||||
monthS = cal.get(Calendar.MONTH)+1
|
||||
yearS = cal.get(Calendar.YEAR)
|
||||
changeData()
|
||||
}
|
||||
|
||||
/** "Close" app on back button */
|
||||
override fun onBackPressed() {
|
||||
moveTaskToBack(true)
|
||||
}
|
||||
|
||||
/** It returns the month name */
|
||||
fun getMonth(month: Int): String {
|
||||
val str = DateFormatSymbols().getMonths()[month - 1]
|
||||
return str.capitalize()
|
||||
}
|
||||
|
||||
/** This function replaces texts of main activity to sentence using IDFraseS var
|
||||
*/
|
||||
fun changeData(){
|
||||
IDFraseS = db.getIDFrase(yearS,monthS, dayS)
|
||||
txt_sentence.setText(db.getSentenceFromIDF(IDFraseS))
|
||||
txt_anime.setText(db.getAnimeFromIDF(IDFraseS))
|
||||
//txt_date.setText("Frase de hoy")
|
||||
txt_date.setText("Frase del $dayS de ${getMonth(monthS)}")
|
||||
/* Set fav icon */
|
||||
if (db.getFav(IDFraseS) == true) {
|
||||
btn_fav.setBackgroundResource(R.drawable.star_on);
|
||||
}else{
|
||||
btn_fav.setBackgroundResource(R.drawable.star_off);
|
||||
}
|
||||
}
|
||||
|
||||
/** This changes sentence of day you are seeing */
|
||||
override fun onRewarded(reward: RewardItem) {
|
||||
alert {
|
||||
customView {
|
||||
linearLayout {
|
||||
imageView {
|
||||
imageResource = R.drawable.gato
|
||||
}.lparams(width = matchParent) {
|
||||
margin = dip(4)
|
||||
}
|
||||
}
|
||||
}
|
||||
positiveButton("Cambiar la frase") {
|
||||
/* First remove sentence, then load a new one, and change data */
|
||||
db.delSentence(yearS,monthS, dayS)
|
||||
IDFraseS = db.getIDFrase(yearS,monthS, dayS)
|
||||
changeData()
|
||||
}
|
||||
}.show()
|
||||
}
|
||||
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.main, menu)
|
||||
return super.onCreateOptionsMenu(menu)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean{
|
||||
when (item.getItemId()) {
|
||||
R.id.favs -> {
|
||||
startActivity<FavsActivity>()
|
||||
return true
|
||||
}
|
||||
R.id.anime -> {
|
||||
startActivity<AnimesSelActivity>()
|
||||
return true
|
||||
}
|
||||
R.id.options -> {
|
||||
startActivity<OptsActivity>()
|
||||
return true
|
||||
}
|
||||
R.id.about -> {
|
||||
startActivity<AboutActivity>()
|
||||
return true
|
||||
}
|
||||
R.id.exit -> {
|
||||
moveTaskToBack(true) /* Returns to "desktop" */
|
||||
return super.onOptionsItemSelected(item)
|
||||
}
|
||||
else -> return super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun preloadAd() {
|
||||
var Adr = AdRequest.Builder()
|
||||
Adr.addTestDevice("C0119E16F190120A0481C80734939D71")
|
||||
Adr.addTestDevice("4264F289943AD2719C3F0ECB2CD4556A")
|
||||
Adr.addTestDevice("D0CF3543C15E51623C67081FA8B64C2B")
|
||||
|
||||
val extras = Bundle()
|
||||
extras.putString("max_ad_content_rating", "PG") //Non adult ads
|
||||
|
||||
mRewardedVideoAd.loadAd(this.getString(R.string.admob_videoid), Adr.addNetworkExtrasBundle(AdMobAdapter::class.java, extras).build())
|
||||
}
|
||||
|
||||
}
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import org.jetbrains.anko.*
|
||||
import android.util.Log
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.view.Menu
|
||||
import android.view.View
|
||||
import android.view.MenuItem
|
||||
import java.util.Calendar
|
||||
import java.text.DateFormatSymbols
|
||||
import androidx.viewpager.widget.ViewPager
|
||||
import android.widget.Toast
|
||||
|
||||
/* Ads */
|
||||
import com.google.android.gms.ads.AdRequest
|
||||
import com.google.android.gms.ads.rewarded.RewardedAd;
|
||||
|
||||
|
||||
import android.text.method.ScrollingMovementMethod
|
||||
import com.google.android.gms.ads.LoadAdError
|
||||
import com.google.android.gms.ads.OnUserEarnedRewardListener
|
||||
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
|
||||
import com.jkanetwork.st.frasesdeanime.databinding.ActivityMainBinding
|
||||
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
private var rewardedAd: RewardedAd? = null
|
||||
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
|
||||
|
||||
/* Prefs, database init and calendar data */
|
||||
lateinit var P: Prefs
|
||||
lateinit var db: SQLiteHelper
|
||||
var cal = Calendar.getInstance()
|
||||
var dayS = cal.get(Calendar.DAY_OF_MONTH)
|
||||
var monthS = cal.get(Calendar.MONTH)+1
|
||||
var yearS = cal.get(Calendar.YEAR)
|
||||
|
||||
var IDFraseS = -2 /* Error if not changes */
|
||||
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
P = Prefs(applicationContext)
|
||||
db = SQLiteHelper(applicationContext)
|
||||
|
||||
|
||||
/* Ads */
|
||||
// Initialize the Mobile Ads SDK.
|
||||
|
||||
var adRequest = AdRequest.Builder().build()
|
||||
RewardedAd.load(this,this.getString(R.string.admob_id), adRequest, object : RewardedAdLoadCallback() {
|
||||
override fun onAdFailedToLoad(adError: LoadAdError) {
|
||||
rewardedAd = null
|
||||
}
|
||||
|
||||
override fun onAdLoaded(ad: RewardedAd) {
|
||||
rewardedAd = ad
|
||||
}
|
||||
})
|
||||
|
||||
val extras = Bundle()
|
||||
|
||||
extras.putString("max_ad_content_rating", "PG") //Non adult ads
|
||||
|
||||
|
||||
/* Check if first time or not, for welcoming user */
|
||||
if (P.getFirstTime() == true){
|
||||
alert("Bienvenido a Frases de anime! Un lugar donde puedes descubrir las frases de tus animes favoritos, todos los días!\n" +
|
||||
"Selecciona de la siguiente lista los que más te gusten para empezar."){
|
||||
positiveButton("Empezemos") {startActivity<AnimesSelActivity>()}
|
||||
}.show()
|
||||
|
||||
}else {
|
||||
/* Check if no anime is selected */
|
||||
if (db.getCountAnimeSel() == 0) {
|
||||
alert("No tienes ningún anime seleccionado, ¿Quieres seleccionarlos ahora?", "No hay animes seleccionados") {
|
||||
positiveButton("Si") { startActivity<AnimesSelActivity>() }
|
||||
negativeButton("No") { }
|
||||
}.show()
|
||||
}
|
||||
}
|
||||
|
||||
//For "Vote us in Play Store"
|
||||
if (P.getRateCountdown() == 0){
|
||||
alert("¿Te gusta la apliación? ¿Crees que la falta algo?"){
|
||||
positiveButton("Danos tu opinión") {browse("https://play.google.com/store/apps/details?id=com.jkanetwork.st.frasesdeanime")}
|
||||
negativeButton("Más tarde") { }
|
||||
}.show()
|
||||
P.setRateCountdown(); //To -1, if not..you know
|
||||
}else{
|
||||
P.setRateCountdown(); //Countdown one
|
||||
}
|
||||
|
||||
|
||||
/* Button listeners */
|
||||
binding.btnFav.setOnClickListener() {
|
||||
db.setFav(IDFraseS)
|
||||
if (db.getFav(IDFraseS)) {
|
||||
binding.btnFav.setBackgroundResource(R.drawable.star_on);
|
||||
}else{
|
||||
binding.btnFav.setBackgroundResource(R.drawable.star_off);
|
||||
}
|
||||
}
|
||||
|
||||
binding.btnShare.setOnClickListener() {
|
||||
share("${binding.txtSentence.text} (${binding.txtAnime.text}) \n ${this.getString(R.string.sharefrom)}")
|
||||
}
|
||||
|
||||
binding.btnChange.setOnClickListener() {
|
||||
|
||||
if (IDFraseS > 0) {
|
||||
rewardedAd?.let { ad ->
|
||||
ad.show(this, OnUserEarnedRewardListener { rewardItem ->
|
||||
// Handle the reward. ANUNCIO VISTO CORRECTAMENTE.
|
||||
alert {
|
||||
customView {
|
||||
linearLayout {
|
||||
imageView {
|
||||
imageResource = R.drawable.gato
|
||||
}.lparams(width = matchParent) {
|
||||
margin = dip(4)
|
||||
}
|
||||
}
|
||||
}
|
||||
positiveButton("Cambiar la frase") {
|
||||
/* First remove sentence, then load a new one, and change data */
|
||||
db.delSentence(yearS,monthS, dayS)
|
||||
IDFraseS = db.getIDFrase(yearS,monthS, dayS)
|
||||
changeData()
|
||||
}
|
||||
}.show()
|
||||
})
|
||||
} ?: run {
|
||||
alert("Aún no hay un anuncio disponible, espere") {
|
||||
positiveButton("Ok") { }
|
||||
}.show()
|
||||
}
|
||||
}else{ // if (IDFraseS <= 0) {
|
||||
alert("Aquí no puedes ver una frase aún, no sirve de nada pulsar el botón") {
|
||||
positiveButton("Ok") { }
|
||||
}.show()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Button for previous day
|
||||
binding.btnPrev.setOnClickListener() {
|
||||
if (dayS == 1){
|
||||
if (monthS == 1){
|
||||
monthS=12
|
||||
yearS -= 1
|
||||
}else{
|
||||
monthS -=1
|
||||
}
|
||||
if (monthS in arrayOf(1, 3, 5,7,8,10,12)){
|
||||
dayS = 31
|
||||
}else if (monthS == 2){
|
||||
dayS = 28
|
||||
}else{
|
||||
dayS = 30
|
||||
}
|
||||
}else{
|
||||
dayS -=1
|
||||
}
|
||||
changeData()
|
||||
}
|
||||
|
||||
//Button of next (day) sentence
|
||||
binding.btnNext.setOnClickListener() {
|
||||
if ((monthS in arrayOf(1, 3, 5,7,8,10) && dayS == 31) || (monthS == 2 && dayS == 28) || (monthS in arrayOf(2,4,6,9,11) && dayS == 30)){
|
||||
monthS += 1
|
||||
dayS = 1
|
||||
}else if (dayS == 31 && monthS == 12){ //New Year
|
||||
monthS = 1
|
||||
dayS = 1
|
||||
}else{
|
||||
dayS +=1
|
||||
}
|
||||
changeData()
|
||||
}
|
||||
|
||||
binding.btnToday.setOnClickListener(){
|
||||
//Go back to today
|
||||
dayS = cal.get(Calendar.DAY_OF_MONTH)
|
||||
monthS = cal.get(Calendar.MONTH)+1
|
||||
yearS = cal.get(Calendar.YEAR)
|
||||
changeData()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** At onResume, called after onCreate and in resume, load ad things and refresh calendar data */
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
binding.txtSentence.setMovementMethod(ScrollingMovementMethod()) //Make sentence text scrollable
|
||||
|
||||
cal = Calendar.getInstance() //Refresh calendar
|
||||
//Refresh day
|
||||
dayS = cal.get(Calendar.DAY_OF_MONTH)
|
||||
monthS = cal.get(Calendar.MONTH)+1
|
||||
yearS = cal.get(Calendar.YEAR)
|
||||
changeData()
|
||||
}
|
||||
|
||||
/** "Close" app on back button */
|
||||
override fun onBackPressed() {
|
||||
moveTaskToBack(true)
|
||||
}
|
||||
|
||||
/** It returns the month name */
|
||||
fun getMonth(month: Int): String {
|
||||
val str = DateFormatSymbols().getMonths()[month - 1]
|
||||
return str.capitalize()
|
||||
}
|
||||
|
||||
/** This function replaces texts of main activity to sentence using IDFraseS var
|
||||
*/
|
||||
fun changeData(){
|
||||
IDFraseS = db.getIDFrase(yearS,monthS, dayS)
|
||||
binding.txtSentence.setText(db.getSentenceFromIDF(IDFraseS))
|
||||
binding.txtAnime.setText(db.getAnimeFromIDF(IDFraseS))
|
||||
//txt_date.setText("Frase de hoy")
|
||||
binding.txtDate.setText("Frase del $dayS de ${getMonth(monthS)}")
|
||||
/* Set fav icon */
|
||||
if (db.getFav(IDFraseS)) {
|
||||
binding.btnFav.setBackgroundResource(R.drawable.star_on);
|
||||
}else{
|
||||
binding.btnFav.setBackgroundResource(R.drawable.star_off);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.main, menu)
|
||||
return super.onCreateOptionsMenu(menu)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean{
|
||||
when (item.getItemId()) {
|
||||
R.id.favs -> {
|
||||
startActivity<FavsActivity>()
|
||||
return true
|
||||
}
|
||||
R.id.anime -> {
|
||||
startActivity<AnimesSelActivity>()
|
||||
return true
|
||||
}
|
||||
R.id.options -> {
|
||||
startActivity<OptsActivity>()
|
||||
return true
|
||||
}
|
||||
R.id.about -> {
|
||||
startActivity<AboutActivity>()
|
||||
return true
|
||||
}
|
||||
R.id.exit -> {
|
||||
moveTaskToBack(true) /* Returns to "desktop" */
|
||||
return super.onOptionsItemSelected(item)
|
||||
}
|
||||
else -> return super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,99 +1,100 @@
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.content.Context
|
||||
import android.widget.ArrayAdapter
|
||||
import org.jetbrains.anko.*
|
||||
import kotlinx.android.synthetic.main.activity_opts.*
|
||||
import android.content.Intent
|
||||
import java.util.*
|
||||
import android.content.BroadcastReceiver
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 30/08/17.
|
||||
*/
|
||||
class OptsActivity : AppCompatActivity(){
|
||||
|
||||
lateinit var P: Prefs
|
||||
|
||||
val cont = this
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_opts)
|
||||
|
||||
P = Prefs(applicationContext)
|
||||
|
||||
val hours = ArrayList<Int>()
|
||||
val minutes = ArrayList<Int>()
|
||||
|
||||
for (i in 0..23){
|
||||
hours.add(i)
|
||||
}
|
||||
for (i in 0..59){
|
||||
minutes.add(i)
|
||||
}
|
||||
|
||||
var hoursAdapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,hours)
|
||||
var minutesAdapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,minutes)
|
||||
|
||||
hoursAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
minutesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
|
||||
spinnerHour.adapter = hoursAdapter
|
||||
spinnerMinute.adapter = minutesAdapter
|
||||
|
||||
spinnerHour.setSelection(P.getHour())
|
||||
spinnerMinute.setSelection(P.getMinute())
|
||||
|
||||
chk_alarm.setChecked(P.getNotifOn())
|
||||
chk_persistent.setChecked(P.getPersNotifOn())
|
||||
|
||||
setEnabledParts()
|
||||
|
||||
/* Button listeners */
|
||||
chk_alarm.setOnClickListener() {
|
||||
setEnabledParts()
|
||||
}
|
||||
chk_persistent.setOnClickListener() {
|
||||
setEnabledParts()
|
||||
}
|
||||
|
||||
btn_SaveNotif.setOnClickListener() {
|
||||
|
||||
P.setHour(spinnerHour.getSelectedItem().toString().toInt())
|
||||
P.setMinute(spinnerMinute.getSelectedItem().toString().toInt())
|
||||
|
||||
P.setNotifOn(chk_alarm.isChecked())
|
||||
P.setPersNotifOn(chk_persistent.isChecked())
|
||||
|
||||
val nm = applicationContext.notificationManager
|
||||
nm.cancel(1); //Canel notif before service going on, for persistent notification problems
|
||||
putAlarm(this)
|
||||
if (chk_persistent.isChecked() && chk_alarm.isChecked()) {
|
||||
this.startService(Intent(this, AlarmNotif::class.java))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun setEnabledParts() {
|
||||
if (chk_alarm.isChecked()) {
|
||||
chk_persistent.setEnabled(true)
|
||||
/* This other checks is chk_persistent checked dependent */
|
||||
if (chk_persistent.isChecked()) {
|
||||
spinnerHour.setEnabled(false)
|
||||
spinnerMinute.setEnabled(false)
|
||||
} else {
|
||||
spinnerHour.setEnabled(true)
|
||||
spinnerMinute.setEnabled(true)
|
||||
}
|
||||
} else {
|
||||
chk_persistent.setEnabled(false)
|
||||
spinnerHour.setEnabled(false)
|
||||
spinnerMinute.setEnabled(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.widget.ArrayAdapter
|
||||
import org.jetbrains.anko.*
|
||||
import android.content.Intent
|
||||
import java.util.*
|
||||
import android.content.BroadcastReceiver
|
||||
import com.jkanetwork.st.frasesdeanime.databinding.ActivityOptsBinding
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 30/08/17.
|
||||
*/
|
||||
class OptsActivity : AppCompatActivity(){
|
||||
|
||||
private lateinit var binding: ActivityOptsBinding
|
||||
lateinit var P: Prefs
|
||||
|
||||
val cont = this
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityOptsBinding.inflate(layoutInflater)
|
||||
setContentView(R.layout.activity_opts)
|
||||
|
||||
P = Prefs(applicationContext)
|
||||
|
||||
val hours = ArrayList<Int>()
|
||||
val minutes = ArrayList<Int>()
|
||||
|
||||
for (i in 0..23){
|
||||
hours.add(i)
|
||||
}
|
||||
for (i in 0..59){
|
||||
minutes.add(i)
|
||||
}
|
||||
|
||||
var hoursAdapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,hours)
|
||||
var minutesAdapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,minutes)
|
||||
|
||||
hoursAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
minutesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
|
||||
binding.spinnerHour.adapter = hoursAdapter
|
||||
binding.spinnerMinute.adapter = minutesAdapter
|
||||
|
||||
binding.spinnerHour.setSelection(P.getHour())
|
||||
binding.spinnerMinute.setSelection(P.getMinute())
|
||||
|
||||
binding.chkAlarm.setChecked(P.getNotifOn())
|
||||
binding.chkPersistent.setChecked(P.getPersNotifOn())
|
||||
|
||||
setEnabledParts()
|
||||
|
||||
/* Button listeners */
|
||||
binding.chkAlarm.setOnClickListener() {
|
||||
setEnabledParts()
|
||||
}
|
||||
binding.chkPersistent.setOnClickListener() {
|
||||
setEnabledParts()
|
||||
}
|
||||
|
||||
binding.btnSaveNotif.setOnClickListener() {
|
||||
|
||||
P.setHour(binding.spinnerHour.getSelectedItem().toString().toInt())
|
||||
P.setMinute(binding.spinnerMinute.getSelectedItem().toString().toInt())
|
||||
|
||||
P.setNotifOn(binding.chkAlarm.isChecked())
|
||||
P.setPersNotifOn(binding.chkPersistent.isChecked())
|
||||
|
||||
val nm = applicationContext.notificationManager
|
||||
nm.cancel(1); //Canel notif before service going on, for persistent notification problems
|
||||
putAlarm(this)
|
||||
if (binding.chkPersistent.isChecked() && binding.chkAlarm.isChecked()) {
|
||||
this.startService(Intent(this, AlarmNotif::class.java))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun setEnabledParts() {
|
||||
if (binding.chkAlarm.isChecked()) {
|
||||
binding.chkPersistent.setEnabled(true)
|
||||
/* This other checks is chk_persistent checked dependent */
|
||||
if (binding.chkPersistent.isChecked()) {
|
||||
binding.spinnerHour.setEnabled(false)
|
||||
binding.spinnerMinute.setEnabled(false)
|
||||
} else {
|
||||
binding.spinnerHour.setEnabled(true)
|
||||
binding.spinnerMinute.setEnabled(true)
|
||||
}
|
||||
} else {
|
||||
binding.chkPersistent.setEnabled(false)
|
||||
binding.spinnerHour.setEnabled(false)
|
||||
binding.spinnerMinute.setEnabled(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,272 +1,272 @@
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import org.jetbrains.anko.db.*
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.util.Calendar
|
||||
import android.util.Log
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 18/07/17.
|
||||
*/
|
||||
|
||||
/** ctx, DB.NAME, null, DB.Version */
|
||||
class SQLiteHelper(ctx: Context) : ManagedSQLiteOpenHelper(ctx, "otaku.sqlite", null, 8) {
|
||||
val context = ctx
|
||||
val db = this.getWritableDatabase()
|
||||
|
||||
// Hacer que los números al pasar a String tengan X digitos
|
||||
fun Int.format(digits: Int) = java.lang.String.format("%0${digits}d", this)
|
||||
companion object {
|
||||
private var instance: SQLiteHelper? = null
|
||||
|
||||
@Synchronized
|
||||
fun getInstance(ctx: Context): SQLiteHelper {
|
||||
if (instance == null) {
|
||||
instance = SQLiteHelper(ctx.applicationContext)
|
||||
}
|
||||
return instance!!
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(db: SQLiteDatabase) {
|
||||
// Here you create tables
|
||||
db.execSQL("CREATE TABLE FRASESVI (" +
|
||||
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
|
||||
"TDay TEXT NOT NULL UNIQUE," +
|
||||
"IDFrase INTEGER)")
|
||||
db.createTable("FRASESFAV", true,
|
||||
"IDFrase" to INTEGER + UNIQUE)
|
||||
db.createTable("ANIMESON", true,
|
||||
"IDAnime" to INTEGER + UNIQUE)
|
||||
|
||||
//Load animes and sentences
|
||||
var queries: List<String> = readSqldata(R.raw.loadsqlfile).split(";\n");
|
||||
for (query in queries){
|
||||
if (!query.isBlank())
|
||||
db.execSQL(query);
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||
// Here you can upgrade tables, as usual
|
||||
var queries: List<String> = readSqldata(R.raw.loadsqlfile).split(";\n");
|
||||
for (query in queries){
|
||||
if (!query.isBlank())
|
||||
db.execSQL(query);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun readSqldata(res: Int): String {
|
||||
//getting the file
|
||||
val inputStream: InputStream
|
||||
inputStream = context.getResources().openRawResource(res)
|
||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||
try {
|
||||
var i = inputStream.read()
|
||||
while (i != -1) {
|
||||
byteArrayOutputStream.write(i)
|
||||
i = inputStream.read()
|
||||
}
|
||||
inputStream.close()
|
||||
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return byteArrayOutputStream.toString()
|
||||
}
|
||||
|
||||
|
||||
/** Returns a boolean saying if a sentence is faved or not */
|
||||
fun getFav (IDFrase: Int): Boolean {
|
||||
val query = db.rawQuery("SELECT IDFrase FROM FRASESFAV WHERE IDFrase = '$IDFrase'", null)
|
||||
val res = query.getCount()
|
||||
query.close()
|
||||
if (res > 0){
|
||||
return true
|
||||
}else{
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/** Changes the state of fav of a sentence
|
||||
* @param IDFrase: Sentence
|
||||
*/
|
||||
fun setFav (IDFrase: Int) {
|
||||
if (getFav(IDFrase) == false){
|
||||
db.insert("FRASESFAV","IDFrase" to IDFrase)
|
||||
}else{
|
||||
db.execSQL("DELETE FROM FRASESFAV WHERE IDFrase='$IDFrase'")
|
||||
}
|
||||
}
|
||||
|
||||
fun getSentenceFromIDF (IDFrase: Int): String {
|
||||
if (IDFrase == 0){ return "No hay más frases disponibles..."}
|
||||
if (IDFrase == -1){ return "No puedes ver aún esta frase"}
|
||||
if (IDFrase == -2){ return "Error inesperado"}
|
||||
val c = db.rawQuery("SELECT Frase FROM FRASES WHERE IDFrase = '$IDFrase'", null)
|
||||
c.moveToFirst()
|
||||
val ret = c.getString(0)
|
||||
c.close()
|
||||
return ret
|
||||
}
|
||||
|
||||
fun getAnimeFromIDF (IDFrase: Int): String {
|
||||
if (IDFrase <= 0){ return "..."}
|
||||
val c = db.rawQuery("SELECT Name FROM ANIMES WHERE IDAnime = (SELECT IDAnime FROM FRASES WHERE IDFrase = '$IDFrase')", null)
|
||||
c.moveToFirst()
|
||||
val ret = c.getString(0)
|
||||
c.close()
|
||||
return ret
|
||||
}
|
||||
|
||||
fun getIDAnimeFromName (Name: String): Int {
|
||||
var query = db.select("ANIMES", "IDAnime").whereArgs("Name = '$Name'")
|
||||
return query.parseSingle(IntParser)
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun getIDFrase (year: Int,month: Int, day: Int): Int {
|
||||
val cal = Calendar.getInstance()
|
||||
/* First Integer to Strings of format YYYYMMDD for store and compare */
|
||||
val diaArg = "${year}${month.format(2)}${day.format(2)}"
|
||||
val diaAct = "${cal.get(Calendar.YEAR)}${(cal.get(Calendar.MONTH)+1).format(2)}${cal.get(Calendar.DAY_OF_MONTH).format(2)}"
|
||||
|
||||
/* Query db looking for the sentence. If there are not loaded yet, see (else if) if they can show a new one, if criteria is true. If not, not show sentence */
|
||||
var query = db.rawQuery("SELECT IDFrase FROM FRASESVI WHERE TDay = '$diaArg'", null)
|
||||
if (query.getCount() > 0){
|
||||
query.moveToFirst()
|
||||
val ret = query.getInt(0)
|
||||
query.close()
|
||||
return ret
|
||||
}else if (diaArg == diaAct || diaArg.toInt() < diaAct.toInt() && db.rawQuery("SELECT IDFrase FROM FRASESVI WHERE Tday < '$diaArg'", null).count > 0 ) {
|
||||
try {
|
||||
query = db.rawQuery("SELECT IDFrase FROM FRASES WHERE IDAnime IN (SELECT IDAnime FROM ANIMESON) AND IDFrase NOT IN (SELECT IDFrase FROM FRASESVI) ORDER BY RANDOM() LIMIT 1", null)
|
||||
query.moveToFirst()
|
||||
db.insert("FRASESVI",
|
||||
"TDay" to diaArg,
|
||||
"IDFrase" to query.getString(0))
|
||||
val ret = query.getInt(0)
|
||||
query.close()
|
||||
return ret
|
||||
}
|
||||
catch (e: Exception) {
|
||||
return 0 //No more sentences
|
||||
}
|
||||
}else{
|
||||
return -1 //Day not yet
|
||||
}
|
||||
}
|
||||
|
||||
fun getIDFraseHoy(): Int {
|
||||
val cal = Calendar.getInstance()
|
||||
//Month + 1 because Android starts with month=0
|
||||
return getIDFrase(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH))
|
||||
}
|
||||
|
||||
fun getAllIDFavs(): Array<Int>{
|
||||
var query = db.select("FRASESFAV", "IDFrase")
|
||||
var list = query.parseList(IntParser)
|
||||
return list.toTypedArray()
|
||||
}
|
||||
|
||||
fun getAllAnimeIDs(): Array<Int>{
|
||||
var query = db.select("ANIMES", "IDAnime").orderBy("Name", SqlOrderDirection.ASC)
|
||||
var list = query.parseList(IntParser)
|
||||
return list.toTypedArray()
|
||||
}
|
||||
|
||||
fun getAllAnimeNames(): Array<String>{
|
||||
var query = db.select("ANIMES", "Name").orderBy("Name", SqlOrderDirection.ASC)
|
||||
var list = query.parseList(StringParser)
|
||||
return list.toTypedArray()
|
||||
}
|
||||
|
||||
/** Return if the anime asked by [IDA] is selected */
|
||||
fun getAnimeSel(IDA: Int): Boolean{
|
||||
val query = db.rawQuery("SELECT IDAnime FROM ANIMESON WHERE IDAnime = '$IDA'", null)
|
||||
val res = query.getCount()
|
||||
query.close()
|
||||
if (res > 0){
|
||||
return true
|
||||
}else{
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/** Changes the state of a selected or not anime
|
||||
* @param IDAnime: Anime
|
||||
*/
|
||||
fun setAnimeSel (IDAnime: Int) {
|
||||
if (getAnimeSel(IDAnime) == false){
|
||||
db.insert("ANIMESON","IDAnime" to IDAnime)
|
||||
}else{
|
||||
db.execSQL("DELETE FROM ANIMESON WHERE IDAnime='$IDAnime'")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Return how much animes are selected (For knowing if 0) */
|
||||
fun getCountAnimeSel(): Int{
|
||||
val query = db.rawQuery("SELECT COUNT(*) FROM ANIMESON", null)
|
||||
query.moveToFirst()
|
||||
val ret = query.getInt(0)
|
||||
query.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** Deletes the sentence of the day passed as argument yearS,monthS, dayS
|
||||
* @param year: Year selected
|
||||
* @param month: Month selected
|
||||
* @param day: Day selected
|
||||
*/
|
||||
fun delSentence (year: Int, month: Int, day: Int) {
|
||||
val diaArg = "${year}${month.format(2)}${day.format(2)}"
|
||||
db.execSQL("DELETE FROM FRASESVI WHERE TDay='$diaArg'")
|
||||
}
|
||||
|
||||
|
||||
/** This funcion selects or desselects all animes from selected to show */
|
||||
fun setAllAnimeSel(){
|
||||
var selectAll = getAllAnimeSel(); //Know if anyone is select (true/false)
|
||||
//First, unset all, and after that, select all if they were not before. (Why? Because if someone are select, inserting again will crash)
|
||||
db.execSQL("DELETE FROM ANIMESON")
|
||||
if (selectAll == false){ //Set all
|
||||
var animeIDs = getAllAnimeIDs()
|
||||
|
||||
for (pos in animeIDs){
|
||||
db.insert("ANIMESON",
|
||||
"IDAnime" to animeIDs[pos-1]) //Pos is a count, arrays start at 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** It returns if all animes are selected or not */
|
||||
fun getAllAnimeSel(): Boolean{
|
||||
var set = getCountAnimeSel();
|
||||
var countanime = getAllAnimeIDs().size;
|
||||
if (set == countanime){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** It returns to program that this sentence can't be seen */
|
||||
fun getSentenceCanBeSeen(year: Int,month: Int, day: Int): Boolean {
|
||||
//TODO, return 0 when if check this sentence will be "nothing", for disable button
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Access property for Context
|
||||
val Context.database: SQLiteHelper
|
||||
package com.jkanetwork.st.frasesdeanime
|
||||
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import org.jetbrains.anko.db.*
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.util.Calendar
|
||||
import android.util.Log
|
||||
|
||||
/**
|
||||
* Created by kprkpr on 18/07/17.
|
||||
*/
|
||||
|
||||
/** ctx, DB.NAME, null, DB.Version */
|
||||
class SQLiteHelper(ctx: Context) : ManagedSQLiteOpenHelper(ctx, "otaku.sqlite", null, 8) {
|
||||
val context = ctx
|
||||
val db = this.getWritableDatabase()
|
||||
|
||||
// Hacer que los números al pasar a String tengan X digitos
|
||||
fun Int.format(digits: Int) = java.lang.String.format("%0${digits}d", this)
|
||||
companion object {
|
||||
private var instance: SQLiteHelper? = null
|
||||
|
||||
@Synchronized
|
||||
fun getInstance(ctx: Context): SQLiteHelper {
|
||||
if (instance == null) {
|
||||
instance = SQLiteHelper(ctx.applicationContext)
|
||||
}
|
||||
return instance!!
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(db: SQLiteDatabase) {
|
||||
// Here you create tables
|
||||
db.execSQL("CREATE TABLE FRASESVI (" +
|
||||
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
|
||||
"TDay TEXT NOT NULL UNIQUE," +
|
||||
"IDFrase INTEGER)")
|
||||
db.createTable("FRASESFAV", true,
|
||||
"IDFrase" to INTEGER + UNIQUE)
|
||||
db.createTable("ANIMESON", true,
|
||||
"IDAnime" to INTEGER + UNIQUE)
|
||||
|
||||
//Load animes and sentences
|
||||
var queries: List<String> = readSqldata(R.raw.loadsqlfile).split(";\n");
|
||||
for (query in queries){
|
||||
if (!query.isBlank())
|
||||
db.execSQL(query);
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||
// Here you can upgrade tables, as usual
|
||||
var queries: List<String> = readSqldata(R.raw.loadsqlfile).split(";\n");
|
||||
for (query in queries){
|
||||
if (!query.isBlank())
|
||||
db.execSQL(query);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun readSqldata(res: Int): String {
|
||||
//getting the file
|
||||
val inputStream: InputStream
|
||||
inputStream = context.getResources().openRawResource(res)
|
||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||
try {
|
||||
var i = inputStream.read()
|
||||
while (i != -1) {
|
||||
byteArrayOutputStream.write(i)
|
||||
i = inputStream.read()
|
||||
}
|
||||
inputStream.close()
|
||||
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return byteArrayOutputStream.toString()
|
||||
}
|
||||
|
||||
|
||||
/** Returns a boolean saying if a sentence is faved or not */
|
||||
fun getFav (IDFrase: Int): Boolean {
|
||||
val query = db.rawQuery("SELECT IDFrase FROM FRASESFAV WHERE IDFrase = '$IDFrase'", null)
|
||||
val res = query.getCount()
|
||||
query.close()
|
||||
if (res > 0){
|
||||
return true
|
||||
}else{
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/** Changes the state of fav of a sentence
|
||||
* @param IDFrase: Sentence
|
||||
*/
|
||||
fun setFav (IDFrase: Int) {
|
||||
if (getFav(IDFrase) == false){
|
||||
db.insert("FRASESFAV","IDFrase" to IDFrase)
|
||||
}else{
|
||||
db.execSQL("DELETE FROM FRASESFAV WHERE IDFrase='$IDFrase'")
|
||||
}
|
||||
}
|
||||
|
||||
fun getSentenceFromIDF (IDFrase: Int): String {
|
||||
if (IDFrase == 0){ return "No hay más frases disponibles..."}
|
||||
if (IDFrase == -1){ return "No puedes ver aún esta frase"}
|
||||
if (IDFrase == -2){ return "Error inesperado"}
|
||||
val c = db.rawQuery("SELECT Frase FROM FRASES WHERE IDFrase = '$IDFrase'", null)
|
||||
c.moveToFirst()
|
||||
val ret = c.getString(0)
|
||||
c.close()
|
||||
return ret
|
||||
}
|
||||
|
||||
fun getAnimeFromIDF (IDFrase: Int): String {
|
||||
if (IDFrase <= 0){ return "..."}
|
||||
val c = db.rawQuery("SELECT Name FROM ANIMES WHERE IDAnime = (SELECT IDAnime FROM FRASES WHERE IDFrase = '$IDFrase')", null)
|
||||
c.moveToFirst()
|
||||
val ret = c.getString(0)
|
||||
c.close()
|
||||
return ret
|
||||
}
|
||||
|
||||
fun getIDAnimeFromName (Name: String): Int {
|
||||
var query = db.select("ANIMES", "IDAnime").whereArgs("Name = '$Name'")
|
||||
return query.parseSingle(IntParser)
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun getIDFrase (year: Int,month: Int, day: Int): Int {
|
||||
val cal = Calendar.getInstance()
|
||||
/* First Integer to Strings of format YYYYMMDD for store and compare */
|
||||
val diaArg = "${year}${month.format(2)}${day.format(2)}"
|
||||
val diaAct = "${cal.get(Calendar.YEAR)}${(cal.get(Calendar.MONTH)+1).format(2)}${cal.get(Calendar.DAY_OF_MONTH).format(2)}"
|
||||
|
||||
/* Query db looking for the sentence. If there are not loaded yet, see (else if) if they can show a new one, if criteria is true. If not, not show sentence */
|
||||
var query = db.rawQuery("SELECT IDFrase FROM FRASESVI WHERE TDay = '$diaArg'", null)
|
||||
if (query.getCount() > 0){
|
||||
query.moveToFirst()
|
||||
val ret = query.getInt(0)
|
||||
query.close()
|
||||
return ret
|
||||
}else if (diaArg == diaAct || diaArg.toInt() < diaAct.toInt() && db.rawQuery("SELECT IDFrase FROM FRASESVI WHERE Tday < '$diaArg'", null).count > 0 ) {
|
||||
try {
|
||||
query = db.rawQuery("SELECT IDFrase FROM FRASES WHERE IDAnime IN (SELECT IDAnime FROM ANIMESON) AND IDFrase NOT IN (SELECT IDFrase FROM FRASESVI) ORDER BY RANDOM() LIMIT 1", null)
|
||||
query.moveToFirst()
|
||||
db.insert("FRASESVI",
|
||||
"TDay" to diaArg,
|
||||
"IDFrase" to query.getString(0))
|
||||
val ret = query.getInt(0)
|
||||
query.close()
|
||||
return ret
|
||||
}
|
||||
catch (e: Exception) {
|
||||
return 0 //No more sentences
|
||||
}
|
||||
}else{
|
||||
return -1 //Day not yet
|
||||
}
|
||||
}
|
||||
|
||||
fun getIDFraseHoy(): Int {
|
||||
val cal = Calendar.getInstance()
|
||||
//Month + 1 because Android starts with month=0
|
||||
return getIDFrase(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH))
|
||||
}
|
||||
|
||||
fun getAllIDFavs(): Array<Int>{
|
||||
var query = db.select("FRASESFAV", "IDFrase")
|
||||
var list = query.parseList(IntParser)
|
||||
return list.toTypedArray()
|
||||
}
|
||||
|
||||
fun getAllAnimeIDs(): Array<Int>{
|
||||
var query = db.select("ANIMES", "IDAnime").orderBy("Name", SqlOrderDirection.ASC)
|
||||
var list = query.parseList(IntParser)
|
||||
return list.toTypedArray()
|
||||
}
|
||||
|
||||
fun getAllAnimeNames(): Array<String>{
|
||||
var query = db.select("ANIMES", "Name").orderBy("Name", SqlOrderDirection.ASC)
|
||||
var list = query.parseList(StringParser)
|
||||
return list.toTypedArray()
|
||||
}
|
||||
|
||||
/** Return if the anime asked by [IDA] is selected */
|
||||
fun getAnimeSel(IDA: Int): Boolean{
|
||||
val query = db.rawQuery("SELECT IDAnime FROM ANIMESON WHERE IDAnime = '$IDA'", null)
|
||||
val res = query.getCount()
|
||||
query.close()
|
||||
if (res > 0){
|
||||
return true
|
||||
}else{
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/** Changes the state of a selected or not anime
|
||||
* @param IDAnime: Anime
|
||||
*/
|
||||
fun setAnimeSel (IDAnime: Int) {
|
||||
if (getAnimeSel(IDAnime) == false){
|
||||
db.insert("ANIMESON","IDAnime" to IDAnime)
|
||||
}else{
|
||||
db.execSQL("DELETE FROM ANIMESON WHERE IDAnime='$IDAnime'")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Return how much animes are selected (For knowing if 0) */
|
||||
fun getCountAnimeSel(): Int{
|
||||
val query = db.rawQuery("SELECT COUNT(*) FROM ANIMESON", null)
|
||||
query.moveToFirst()
|
||||
val ret = query.getInt(0)
|
||||
query.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** Deletes the sentence of the day passed as argument yearS,monthS, dayS
|
||||
* @param year: Year selected
|
||||
* @param month: Month selected
|
||||
* @param day: Day selected
|
||||
*/
|
||||
fun delSentence (year: Int, month: Int, day: Int) {
|
||||
val diaArg = "${year}${month.format(2)}${day.format(2)}"
|
||||
db.execSQL("DELETE FROM FRASESVI WHERE TDay='$diaArg'")
|
||||
}
|
||||
|
||||
|
||||
/** This funcion selects or desselects all animes from selected to show */
|
||||
fun setAllAnimeSel(){
|
||||
var selectAll = getAllAnimeSel(); //Know if anyone is select (true/false)
|
||||
//First, unset all, and after that, select all if they were not before. (Why? Because if someone are select, inserting again will crash)
|
||||
db.execSQL("DELETE FROM ANIMESON")
|
||||
if (selectAll == false){ //Set all
|
||||
var animeIDs = getAllAnimeIDs()
|
||||
|
||||
for (pos in animeIDs){
|
||||
db.insert("ANIMESON",
|
||||
"IDAnime" to animeIDs[pos-1]) //Pos is a count, arrays start at 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** It returns if all animes are selected or not */
|
||||
fun getAllAnimeSel(): Boolean{
|
||||
var set = getCountAnimeSel();
|
||||
var countanime = getAllAnimeIDs().size;
|
||||
if (set == countanime){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** It returns to program that this sentence can't be seen */
|
||||
fun getSentenceCanBeSeen(year: Int,month: Int, day: Int): Boolean {
|
||||
//TODO, return 0 when if check this sentence will be "nothing", for disable button
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Access property for Context
|
||||
val Context.database: SQLiteHelper
|
||||
get() = SQLiteHelper.getInstance(applicationContext)
|
||||
+896
-896
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user