Commit d3b6582a authored by Javier Romero's avatar Javier Romero

Fixed some static analysis checks

parent 00dd8a38
......@@ -11,6 +11,8 @@ import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.gitlab.connect2sql.R;
public class QuickKeysAdapter extends BaseExpandableListAdapter {
public static final int SECTION_SNIPPETS = 0;
......@@ -116,19 +118,19 @@ public class QuickKeysAdapter extends BaseExpandableListAdapter {
switch (groupPosition) {
case SECTION_COLUMNS:
textView.setText("Columns");
textView.setText(R.string.qk_section_column);
break;
case SECTION_DATABASES:
textView.setText("Databases");
textView.setText(R.string.qk_section_databases);
break;
case SECTION_TABLES:
textView.setText("Tables");
textView.setText(R.string.qk_section_tables);
break;
case SECTION_SNIPPETS:
textView.setText("Snippets");
textView.setText(R.string.qk_section_snippets);
break;
case SECTION_OPERATORS:
textView.setText("Operators");
textView.setText(R.string.qk_section_operators);
break;
default:
textView.setText("");
......
......@@ -14,6 +14,8 @@ import app.devlife.connect2sql.db.model.query.SavedQuery
class SavedQueriesAdapter(private val context: Context) : BaseExpandableListAdapter() {
var titleOnly = false
private val inflator = LayoutInflater.from(context)
private val builtInText = context.getString(R.string.saved_queries_built_in)
private val savedText = context.getString(R.string.saved_queries_built_in)
private val queries: List<MutableList<BaseNamedQuery>> = listOf(arrayListOf(), arrayListOf())
fun addToBuiltInQueries(query: BuiltInQuery) {
......@@ -100,8 +102,8 @@ class SavedQueriesAdapter(private val context: Context) : BaseExpandableListAdap
val textView = view.findViewById(android.R.id.text1) as TextView
when (groupPosition) {
GROUP_BUILTIN -> textView.text = "Built In"
else -> textView.text = "Saved"
GROUP_BUILTIN -> textView.text = builtInText
else -> textView.text = savedText
}
return view
......@@ -116,9 +118,7 @@ class SavedQueriesAdapter(private val context: Context) : BaseExpandableListAdap
}
companion object {
// This is basically the position of each group
val GROUP_BUILTIN = 0
val GROUP_SAVED = 1
const val GROUP_BUILTIN = 0
const val GROUP_SAVED = 1
}
}
package app.devlife.connect2sql.adapter;
import java.util.List;
import com.gitlab.connect2sql.R;
import app.devlife.connect2sql.sql.Table;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class TableListAdapter extends BaseAdapter {
private final List<Table> tableList;
private final LayoutInflater mInflater;
public TableListAdapter(Context context, List<Table> tableList) {
this.tableList = tableList;
notifyDataSetChanged();
mInflater = LayoutInflater.from(context);
}
public void clear() {
tableList.clear();
notifyDataSetChanged();
}
public void add(Table table) {
tableList.add(table);
notifyDataSetChanged();
}
public List<Table> getTables() {
return tableList;
}
@Override
public int getCount() {
return tableList.size();
}
@Override
public Object getItem(int position) {
return tableList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_table, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.imageView1);
holder.txtName = (TextView) convertView.findViewById(R.id.text1);
holder.txtType = (TextView) convertView.findViewById(R.id.text2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Table table = tableList.get(position);
String name = table.getName();
String typeString = "";
if (table.getType() == Table.TYPE_VIEW) {
typeString = "View";
holder.image.setImageResource(R.drawable.icon_view);
} else {
typeString = "Table";
holder.image.setImageResource(R.drawable.icon_table);
}
holder.txtName.setText(name);
holder.txtType.setText("(" + typeString + ")");
return convertView;
}
private static class ViewHolder {
ImageView image;
TextView txtName;
TextView txtType;
}
}
package app.devlife.connect2sql.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import app.devlife.connect2sql.sql.Table
import com.gitlab.connect2sql.R
class TableListAdapter(private val context: Context, private val tableList: MutableList<Table>) : BaseAdapter() {
private val mInflater: LayoutInflater
val tables: List<Table>
get() = tableList
init {
notifyDataSetChanged()
mInflater = LayoutInflater.from(context)
}
fun clear() {
tableList.clear()
notifyDataSetChanged()
}
fun add(table: Table) {
tableList.add(table)
notifyDataSetChanged()
}
override fun getCount(): Int {
return tableList.size
}
override fun getItem(position: Int): Any {
return tableList[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = convertView ?: mInflater.inflate(R.layout.item_table, parent).apply {
this.tag = ViewHolder(
this.findViewById(R.id.imageView1),
this.findViewById(R.id.text1),
this.findViewById(R.id.text2)
)
}
val holder: ViewHolder = view.tag as ViewHolder
with(tableList[position]) {
holder.txtName.text = this.name
when (this.type) {
Table.TYPE_TABLE -> {
holder.image.setImageResource(R.drawable.icon_table)
holder.txtType.text = context.getString(R.string.table_list_type_table)
}
Table.TYPE_VIEW -> {
holder.image.setImageResource(R.drawable.icon_view)
holder.txtType.text = context.getString(R.string.table_list_type_view)
}
}
}
return view
}
private data class ViewHolder(
val image: ImageView,
val txtName: TextView,
val txtType: TextView
)
}
......@@ -8,41 +8,46 @@ import android.content.SharedPreferences
*/
class UserPreferences(context: Context) {
private val sharedPreferences: SharedPreferences
private val onChangeListeners: MutableMap<(String, Any?) -> Unit, String> = hashMapOf()
private val sharedPrefsChangeListenerDelegate: (SharedPreferences, String) -> Unit = { sharedPreferences, s ->
private val sharedPreferences: SharedPreferences = context.applicationContext.getSharedPreferences(
"user.prefs", Context.MODE_PRIVATE)
private val sharedPrefsChangeListenerDelegate: (SharedPreferences, String) -> Unit = { _, s ->
onChangeListeners.entries.filter { it.value.equals(s) }.forEach { it ->
it.key(s, read(s, null))
}
}
init {
sharedPreferences = context.applicationContext.getSharedPreferences("user.prefs", Context.MODE_PRIVATE)
}
public fun <T> read(key: String, default: T): T {
fun <T> read(key: String, default: T): T {
val entry = sharedPreferences.all.entries.firstOrNull { it.key.equals(key) }
return if (entry == null) default
else if (entry.value == null) default
else entry.value as T
return when {
entry == null -> default
entry.value == null -> default
else -> entry.value as T
}
}
public fun <T> save(option: Option<T>) {
fun <T> save(option: Option<T>) {
when (option) {
is Option.BooleanOption -> sharedPreferences.edit().putBoolean(option.key, option.value).apply()
is Option.LongOption -> sharedPreferences.edit().putLong(option.key, option.value).apply()
is Option.StringOption -> sharedPreferences.edit().putString(option.key, option.value).apply()
is Option.BooleanOption ->
sharedPreferences.edit().putBoolean(option.key, option.value).apply()
is Option.LongOption ->
sharedPreferences.edit().putLong(option.key, option.value).apply()
is Option.StringOption ->
sharedPreferences.edit().putString(option.key, option.value).apply()
}
}
public fun <T> registerListener(key: String, onPreferenceChangeListener: (String, T?) -> Unit) {
fun <T> registerListener(key: String, onPreferenceChangeListener: (String, T?) -> Unit) {
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPrefsChangeListenerDelegate)
onChangeListeners.put(onPreferenceChangeListener as (String, Any?) -> Unit, key)
}
public fun <T> unregisterListener(onPreferenceChangeListener: (String, T?) -> Unit) {
fun <T> unregisterListener(onPreferenceChangeListener: (String, T?) -> Unit) {
onChangeListeners.remove(onPreferenceChangeListener as (String, Any?) -> Unit)
if (onChangeListeners.isEmpty()) sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPrefsChangeListenerDelegate)
if (onChangeListeners.isEmpty()) {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(
sharedPrefsChangeListenerDelegate)
}
}
sealed class Option<T>(val key: String, val value: T) {
......
......@@ -31,7 +31,8 @@ class DefaultDriverAgent(private val driverHelper: DriverHelper) : DriverAgent {
}
}
override fun tables(connection: Connection, databaseName: DriverAgent.Database): Observable<DriverAgent.Table> {
override fun tables(connection: Connection,
databaseName: DriverAgent.Database): Observable<DriverAgent.Table> {
EzLogger.v("[tables] databaseName=$databaseName")
return Observable.create { subscriber ->
try {
......@@ -71,7 +72,9 @@ class DefaultDriverAgent(private val driverHelper: DriverHelper) : DriverAgent {
}
}
override fun columns(connection: Connection, databaseName: DriverAgent.Database, tableName: DriverAgent.Table): Observable<DriverAgent.Column> {
override fun columns(connection: Connection,
databaseName: DriverAgent.Database,
tableName: DriverAgent.Table): Observable<DriverAgent.Column> {
EzLogger.v("[columns] databaseName=$databaseName, tableName=$tableName")
return Observable.create { subscriber ->
try {
......@@ -96,14 +99,19 @@ class DefaultDriverAgent(private val driverHelper: DriverHelper) : DriverAgent {
}
}
override fun execute(connection: Connection, databaseName: DriverAgent.Database?, sql: String): Observable<Statement> {
override fun execute(connection: Connection,
databaseName: DriverAgent.Database?,
sql: String): Observable<Statement> {
return Observable.create { subscriber ->
try {
if (databaseName != null) {
useDatabase(connection, databaseName)
}
val statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)
val statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY)
statement.execute(sql)
subscriber.onNext(statement)
subscriber.onCompleted()
......@@ -113,7 +121,9 @@ class DefaultDriverAgent(private val driverHelper: DriverHelper) : DriverAgent {
}
}
override fun extract(resultSet: ResultSet, startIndex: Int, displayLimit: Int): Observable<DriverAgent.DisplayResults> {
override fun extract(resultSet: ResultSet,
startIndex: Int,
displayLimit: Int): Observable<DriverAgent.DisplayResults> {
return Observable.create { subscriber ->
try {
val metaData = resultSet.metaData
......@@ -123,10 +133,12 @@ class DefaultDriverAgent(private val driverHelper: DriverHelper) : DriverAgent {
val totalRows = resultSet.row
val columnNameAndTypes = (0 until columnCount).map { i -> Pair(
metaData.getColumnLabel(i + 1),
metaData.getColumnType(i + 1)
)}
val columnNameAndTypes = (0 until columnCount).map { i ->
Pair(
metaData.getColumnLabel(i + 1),
metaData.getColumnType(i + 1)
)
}
val startRow = startIndex + 1
val lastDisplayedRow = when {
......@@ -138,7 +150,7 @@ class DefaultDriverAgent(private val driverHelper: DriverHelper) : DriverAgent {
resultSet.absolute(row)
(0 until columnCount).map { columnIndex ->
when(columnNameAndTypes[columnIndex].second) {
when (columnNameAndTypes[columnIndex].second) {
Types.BLOB, Types.LONGVARBINARY -> "[blob]"
else -> resultSet.getString(columnIndex + 1) ?: "[null]"
}
......
package app.devlife.connect2sql.ui.connection.form;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.gitlab.connect2sql.R;
/**
* Created by javier.romero on 5/5/14.
*/
public class ActionBarContainer extends FrameLayout {
private ImageView mLogoView;
private TextView mTitleView;
private View mBackground;
public ActionBarContainer(Context context) {
this(context, null);
}
public ActionBarContainer(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ActionBarContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.form_actionbar, this);
mBackground = view.findViewById(R.id.form_actionbar_background);
mLogoView = (ImageView) view.findViewById(R.id.form_actionbar_logo);
mTitleView = (TextView) view.findViewById(R.id.form_actionbar_title);
}
public ImageView getLogoView() {
return mLogoView;
}
public TextView getTitleView() {
return mTitleView;
}
@Override
public void setBackgroundResource(int resid) {
setBackground(getResources().getDrawable(resid));
}
@Override
public void setBackgroundDrawable(Drawable background) {
setBackground(background);
}
@Override
public void setBackground(Drawable background) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mBackground.setBackground(background);
} else {
mBackground.setBackgroundDrawable(background);
}
}
}
package app.devlife.connect2sql.ui.connection.form
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.gitlab.connect2sql.R
class ActionBarContainer @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0) : FrameLayout(context, attrs, defStyle) {
lateinit var logoView: ImageView
private set
lateinit var titleView: TextView
private set
private lateinit var background: FrameLayout
init {
init()
}
private fun init() {
val view = LayoutInflater.from(context).inflate(R.layout.form_actionbar, this)
background = view.findViewById(R.id.form_actionbar_background)
logoView = view.findViewById(R.id.form_actionbar_logo)
titleView = view.findViewById(R.id.form_actionbar_title)
}
override fun setBackgroundResource(resid: Int) {
background.setBackgroundResource(resid)
}
override fun setBackground(background: Drawable) {
this.background.background = background
}
}
......@@ -5,34 +5,36 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import com.mobsandgeeks.saripaar.Validator
import com.mobsandgeeks.saripaar.annotation.NumberRule
import com.mobsandgeeks.saripaar.annotation.Required
import app.devlife.connect2sql.db.model.connection.ConnectionInfo
import com.gitlab.connect2sql.R
import app.devlife.connect2sql.sql.DriverType
import app.devlife.connect2sql.sql.driver.DriverDefaults
import app.devlife.connect2sql.ui.widget.NotifyingScrollView
import com.gitlab.connect2sql.R
import com.mobsandgeeks.saripaar.Validator
import com.mobsandgeeks.saripaar.annotation.NumberRule
import com.mobsandgeeks.saripaar.annotation.Required
/**
* Created by javier.romero on 4/29/14.
*/
abstract class BaseForm(val context: Context, view: View) {
private val viewGroup: ViewGroup = view as ViewGroup
@Required(order = 1, messageResId = R.string.form_error_name_required)
val nameEditText: EditText
@Required(order = 2, messageResId = R.string.form_error_host_required)
val hostEditText: EditText
@Required(order = 3, messageResId = R.string.form_error_port_required)
@NumberRule(order = 4, messageResId = R.string.form_error_port_required, type = NumberRule.NumberType.INTEGER, gt = 0.0)
@NumberRule(order = 4,
messageResId = R.string.form_error_port_required,
type = NumberRule.NumberType.INTEGER,
gt = 0.0)
val portEditText: EditText
@Required(order = 5, messageResId = R.string.form_error_username_required)
val usernameEditText: EditText
val passwordEditText: EditText
val databaseEditTextView: EditText
private val mView: ViewGroup
val validator: Validator
val testButton: Button
val saveButton: Button
......@@ -40,33 +42,33 @@ abstract class BaseForm(val context: Context, view: View) {
val actionBarContainer: ActionBarContainer
init {
mView = view as ViewGroup
validator = Validator(this)
actionBarContainer = mView.findViewById(R.id.form_actionbar) as ActionBarContainer
scrollView = mView.findViewById(R.id.scroll_view) as NotifyingScrollView
actionBarContainer = viewGroup.findViewById(R.id.form_actionbar) as ActionBarContainer
scrollView = viewGroup.findViewById(R.id.scroll_view) as NotifyingScrollView
nameEditText = mView.findViewById(R.id.form_txt_name) as EditText
nameEditText = viewGroup.findViewById(R.id.form_txt_name) as EditText
hostEditText = view.findViewById(R.id.form_txt_host) as EditText
portEditText = view.findViewById(R.id.form_txt_port) as EditText
usernameEditText = view.findViewById(R.id.form_txt_username) as EditText
passwordEditText = view.findViewById(R.id.form_txt_password) as EditText
databaseEditTextView = view.findViewById(R.id.form_txt_database) as EditText
testButton = mView.findViewById(R.id.form_btn_test) as Button
saveButton = mView.findViewById(R.id.form_btn_save) as Button
testButton = viewGroup.findViewById(R.id.form_btn_test) as Button
saveButton = viewGroup.findViewById(R.id.form_btn_save) as Button
}
val view: View
get() = mView
get() = viewGroup
fun setOnActionOnClickListener(onActionOnClickListener: Field.OnActionClickListener) {
setOnActionOnClickListenerRecursive(mView, onActionOnClickListener)
setOnActionOnClickListenerRecursive(viewGroup, onActionOnClickListener)
}
private fun setOnActionOnClickListenerRecursive(viewGroup: ViewGroup, onActionOnClickListener: Field.OnActionClickListener) {
for (i in 0..viewGroup.childCount - 1) {
private fun setOnActionOnClickListenerRecursive(viewGroup: ViewGroup,
onActionOnClickListener: Field.OnActionClickListener) {
for (i in 0 until viewGroup.childCount) {
val v = viewGroup.getChildAt(i)
if (v is Field) {
v.setOnActionClickListener(onActionOnClickListener)
......@@ -96,27 +98,27 @@ abstract class BaseForm(val context: Context, view: View) {
open fun generateConnectionInfo(): ConnectionInfo {
return ConnectionInfo(
-1,
nameEditText.text.toString(),
DriverType.MYSQL,
hostEditText.text.toString(),
Integer.parseInt(portEditText.text.toString()),
usernameEditText.text.toString(),
passwordEditText.text.toString(),
databaseEditTextView.text.toString(),
hashMapOf())
-1,
nameEditText.text.toString(),
DriverType.MYSQL,
hostEditText.text.toString(),
Integer.parseInt(portEditText.text.toString()),
usernameEditText.text.toString(),
passwordEditText.text.toString(),
databaseEditTextView.text.toString(),
hashMapOf())
}
open fun populate(connectionInfo: ConnectionInfo) {
nameEditText.setText(connectionInfo.name)
hostEditText.setText(connectionInfo.host)
portEditText.setText("" + connectionInfo.port)
portEditText.setText("${connectionInfo.port}")
usernameEditText.setText(connectionInfo.username)
passwordEditText.setText(connectionInfo.password)
databaseEditTextView.setText(connectionInfo.database)
}
fun populate(driverDefaults: DriverDefaults) {
portEditText.setText("" + driverDefaults.port)
portEditText.setText("${driverDefaults.port}")
}
}
......@@ -68,7 +68,7 @@ import javax.inject.Inject
class QueryActivity : BaseActivity() {
private val connectionInfo: ConnectionInfo by lazy {
val id = intent?.extras?.getLong(EXTRA_CONNECTION_INFO_ID).ensure({ t -> t != null && t > 0 })!!
val id = intent?.extras?.getLong(EXTRA_CONNECTION_INFO_ID).ensure { t -> t != null && t > 0 }!!
connectionInfoRepo.getConnectionInfo(id)
}
......@@ -188,7 +188,7 @@ class QueryActivity : BaseActivity() {
mDialogQuickKeys!!.setTitle("Quick Keys")
mDialogQuickKeys!!.setOnShowListener { dialog ->
val d = dialog as Dialog
d.window.setBackgroundDrawable(ColorDrawable(0))
d.window?.setBackgroundDrawable(ColorDrawable(0))
}
val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_quick_keys, null)
......@@ -214,9 +214,13 @@ class QueryActivity : BaseActivity() {
lblCurrentDatabase.setOnClickListener {
if (connectionInfo.driverType == DriverType.POSTGRES) {
Toast.makeText(this@QueryActivity, getString(R.string.error_postgres_changing_databases), Toast.LENGTH_SHORT).show()
Toast.makeText(this@QueryActivity,
getString(R.string.error_postgres_changing_databases),
Toast.LENGTH_SHORT).show()
} else if (databaseAdapter.isEmpty) {
Toast.makeText(this@QueryActivity, getString(R.string.error_no_detected_databases), Toast.LENGTH_SHORT).show()
Toast.makeText(this@QueryActivity,
getString(R.string.error_no_detected_databases),
Toast.LENGTH_SHORT).show()
} else {