Skip to content

Commit

Permalink
Totally converted to Kotlin language, Added samples in Java and Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
hummatli committed Jun 24, 2017
1 parent 1b7359e commit 3dcc1e5
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 25 deletions.
2 changes: 1 addition & 1 deletion MAHAndroidUpdater/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.closeSoftKeyboard;
import static android.support.test.espresso.Espresso.onView;

import static android.support.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.assertion.ViewAssertions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;

import com.mobapphome.mahandroidupdater.sample.MainActivity;
import com.mobapphome.mahandroidupdater.sample.MainActivityKotlin;

@RunWith(AndroidJUnit4.class)
@LargeTest
Expand All @@ -32,8 +28,8 @@ public class MAHButtonClickTest {
//private String mStringToBetyped;

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
MainActivity.class);
public ActivityTestRule<MainActivityKotlin> mActivityRule = new ActivityTestRule<>(
MainActivityKotlin.class);

@Before
public void initValidString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.mobapphome.mahandroidupdater.sample.MainActivity">
<activity android:name="com.mobapphome.mahandroidupdater.sample.MainActivityKotlin">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".MainActivityJava"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ object LocaleHelper {

private val SELECTED_LANGUAGE = "MAHAds.Locale.Helper.Selected.Language"

@JvmStatic
fun onCreate(context: Context, defaultLanguage: String = Locale.getDefault().language) {
val lang = getPersistedData(context, defaultLanguage)
setLocale(context, lang)
}

@JvmStatic
fun getLanguage(context: Context): String {
return getPersistedData(context, Locale.getDefault().language)
}

@JvmStatic
fun setLocale(context: Context, language: String) {
Log.i(Constants.MAH_ANDROID_UPDATER_LOG_TAG, "Language = " + language)
persist(context, language)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package com.mobapphome.mahandroidupdater.sample;

/**
* Created by settar on 6/24/17.
*/

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.mobapphome.mahandroidupdater.tools.Constants;
import com.mobapphome.mahandroidupdater.tools.MAHUpdaterController;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivityJava extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// For MAHUpdater init
MAHUpdaterController.init(this,
"https://project-943403214286171762.firebaseapp.com/mah_android_updater_dir/mah_android_updater_sample.json");
// METHOD 1


//This block is only in Java sample---------------
{
findViewById(R.id.mahBtnOpenJavaSample).setVisibility(View.GONE);
setTitle(getString(R.string.title_java_sample));
}

ImageView imageView = (ImageView) findViewById(R.id.ivMAHForkMeOnGithub);
Drawable forkMeImg = getResources().getDrawable(R.drawable.forkme_green);
// setting the opacity (alpha)
forkMeImg.setAlpha(180);
// setting the images on the ImageViews
imageView.setImageDrawable(forkMeImg);

findViewById(R.id.mahBtnRestricterDlgTest).setOnClickListener(this);
findViewById(R.id.mahBtnUpdaterDlgTest).setOnClickListener(this);
imageView.setOnClickListener(this);

((TextView) findViewById(R.id.tvMAHAULibGithubUrl)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.tvMAHAULibJCenterURL)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.tvMAHAdsLibContrubute)).setMovementMethod(LinkMovementMethod.getInstance());


String[] langsArray = new String[]{
"Azerbaijani",
"English",
"German",
"Hindi",
"Portuguese",
"Russian",
"Turkish"};


Spinner langSpinner = (Spinner) findViewById(R.id.langSpinner);
ArrayAdapter adapter = new ArrayAdapter<>(
this,
android.R.layout.simple_spinner_item,
new ArrayList<CharSequence>(Arrays.asList(langsArray)));

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
langSpinner.setAdapter(adapter);

//Setting local.
LocaleHelper.onCreate(this, "en");
String currentLang = LocaleHelper.getLanguage(this);
if (currentLang.equals("az")) {
currentLang = "azerbaijani";
} else if (currentLang.equals("en")) {
currentLang = "english";
} else if (currentLang.equals("de")) {
currentLang = "german";
} else if (currentLang.equals("hi")) {
currentLang = "hindi";
} else if (currentLang.equals("pt")) {
currentLang = "portuguese";
} else if (currentLang.equals("ru")) {
currentLang = "russian";
} else if (currentLang.equals("tr")) {
currentLang = "turkish";
}

//Setting spinner to right language

for (int i = 0; i < langsArray.length; i++) {
if (langsArray[i].toLowerCase().startsWith(currentLang)) {
langSpinner.setSelection(i);
}

}
langSpinner.setOnItemSelectedListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
protected void onDestroy() {
MAHUpdaterController.end();
super.onDestroy();
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.mahBtnUpdaterDlgTest) {
MAHUpdaterController.testUpdaterDlg(this);
} else if (v.getId() == R.id.mahBtnRestricterDlgTest) {
MAHUpdaterController.testRestricterDlg(this);
} else if (v.getId() == R.id.ivMAHForkMeOnGithub) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.MAH_UPD_GITHUB_LINK));
startActivity(browserIntent);
}
}

//Selection event for language spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String item = parent.getItemAtPosition(pos).toString();

// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item + " id:" + id, Toast.LENGTH_LONG).show();
if (item.toLowerCase().startsWith("azerbaijani")) {
LocaleHelper.setLocale(this, "az");
} else if (item.toLowerCase().startsWith("english")) {
LocaleHelper.setLocale(this, "en");
} else if (item.toLowerCase().startsWith("german")) {
LocaleHelper.setLocale(this, "de");
} else if (item.toLowerCase().startsWith("hindi")) {
LocaleHelper.setLocale(this, "hi");
} else if (item.toLowerCase().startsWith("portuguese")) {
LocaleHelper.setLocale(this, "pt");
} else if (item.toLowerCase().startsWith("russian")) {
LocaleHelper.setLocale(this, "ru");
} else if (item.toLowerCase().startsWith("turkish")) {
LocaleHelper.setLocale(this, "tr");
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ import com.mobapphome.mahandroidupdater.tools.Constants
import com.mobapphome.mahandroidupdater.tools.MAHUpdaterController
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.widget.EditText

class MainActivity : AppCompatActivity() {


class MainActivityKotlin : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -28,6 +32,12 @@ class MainActivity : AppCompatActivity() {
// METHOD 1


//This block is only in Kotlin sample
run {
title = getString(R.string.title_kotlin_sample)
}


val forkMeImg = resources.getDrawable(R.drawable.forkme_green)
// setting the opacity (alpha)
forkMeImg.alpha = 180
Expand All @@ -39,6 +49,10 @@ class MainActivity : AppCompatActivity() {
ivMAHForkMeOnGithub.setOnClickListener {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(Constants.MAH_UPD_GITHUB_LINK)))
}
mahBtnOpenJavaSample.setOnClickListener {
val intent = Intent(this, MainActivityJava::class.java)
startActivity(intent)
}

tvMAHAULibGithubUrl.decorateAsLink()
tvMAHAULibJCenterURL.decorateAsLink()
Expand Down Expand Up @@ -79,7 +93,7 @@ class MainActivity : AppCompatActivity() {


langSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
override fun onItemSelected(parent: AdapterView<*>, view: View?, pos: Int, id: Long) {
val item = parent.getItemAtPosition(pos).toString()

// Showing selected spinner item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
tools:context=".MainActivityKotlin">

<RelativeLayout
android:layout_width="match_parent"
Expand All @@ -26,8 +26,16 @@
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content_txt"
android:textSize="16sp" />
android:text="@string/content_txt1"
android:textSize="14sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content_txt2"
android:textSize="14sp"
android:textColor="@color/colorAccent"
android:layout_marginTop="10dp"/>


<LinearLayout
Expand Down Expand Up @@ -86,6 +94,12 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/txt_btn_open_restricter_dlg" />

<Button
android:id="@+id/mahBtnOpenJavaSample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/txt_btn_open_java_sample" />
</LinearLayout>

<LinearLayout
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mobapphome.mahandroidupdater.sample.MainActivity" >
tools:context="com.mobapphome.mahandroidupdater.sample.MainActivityKotlin" >

<item
android:id="@+id/action_settings"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<resources>
<string name="app_name">MAHAndroidUpdater - Sample</string>
<string name="title_kotlin_sample">Kotlin Sample Activity</string>
<string name="title_java_sample">Java Sample Activity</string>
<string name="action_settings">Settings</string>
<string name="content_txt">
This is the sample for using MAHAndroidUpdater lib. MAHAndroidUpdater is library for updating android apps installed to android device.
\nBy it\'s help old application gets update from Google Play Market. If your app for some
reason suddlnly has deleted from PlayStore, don\'t worry you can provide you new app to old users too.
<string name="content_txt1">
This is the sample for MAHAndroidUpdater, library for notifying users about updates on installed android applications.
\nIf your app for some reason suddlnly has deleted from PlayStore, don\'t worry you can provide you new app to old users too.
</string>
<string name="content_txt2">
Library has totally created by Kotlin language in Android Studio. Contains sample in both Kotlin and Java.
100% compatible with Java and Kotlin applications.
</string>


<string name="txt_lang">Language: </string>
<string name="txt_btn_open_updater_dlg">Test open Updater Dlg</string>
<string name="txt_btn_open_restricter_dlg">Test open Restricter Dlg</string>
<string name="txt_btn_open_java_sample">Open Java Activity</string>
<string name="txt_links">Links:</string>
<string name="txt_note">Note:</string>

Expand Down
Loading

0 comments on commit 3dcc1e5

Please sign in to comment.