mirror of
https://github.com/imcarlost/Android-Itunes-API.git
synced 2026-04-10 02:46:54 -04:00
+ Permissions manager
+ Networking implementation + Design for the ArtistsListActivity - Search icon on the Actionbar (replaced with an EditText)
This commit is contained in:
@@ -30,8 +30,8 @@ dependencies {
|
|||||||
androidTestImplementation 'com.android.support.test:runner:1.0.1'
|
androidTestImplementation 'com.android.support.test:runner:1.0.1'
|
||||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
|
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
|
||||||
|
|
||||||
compile 'com.github.shaz-tech:EasyRuntimePermission:1.0'
|
compile 'com.android.support:design:26.1.0'
|
||||||
compile 'pro.oncreate.easynet:easynet:1.3.0'
|
compile 'com.anthonycr.grant:permissions:1.0'
|
||||||
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
|
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
|
||||||
|
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.hakodev.androiditunesapi">
|
package="com.hakodev.androiditunesapi">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".AndroidItunesAPI"
|
android:name=".AndroidItunesAPI"
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
@@ -9,11 +11,13 @@
|
|||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/AppTheme.BrandedLaunch">
|
android:theme="@style/AppTheme.BrandedLaunch"
|
||||||
|
android:fullBackupContent="@xml/backup_rules">
|
||||||
<activity android:name=".activities.ArtistListActivity">
|
<activity android:name=".activities.ArtistListActivity">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
<action android:name="android.intent.action.VIEW" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|||||||
@@ -2,14 +2,15 @@ package com.hakodev.androiditunesapi;
|
|||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
|
||||||
import pro.oncreate.easynet.EasyNet;
|
import okhttp3.OkHttpClient;
|
||||||
import pro.oncreate.easynet.models.NResponseModel;
|
|
||||||
|
|
||||||
public class AndroidItunesAPI extends Application {
|
public class AndroidItunesAPI extends Application {
|
||||||
|
|
||||||
private static Application instance = null;
|
private static AndroidItunesAPI instance = null;
|
||||||
|
|
||||||
public static Application getInstance() {
|
private OkHttpClient networkClient;
|
||||||
|
|
||||||
|
public static AndroidItunesAPI getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,14 +18,10 @@ public class AndroidItunesAPI extends Application {
|
|||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
instance = this;
|
instance = this;
|
||||||
EasyNet.getInstance()
|
networkClient = new OkHttpClient();
|
||||||
.setWriteLogs(false)
|
|
||||||
.setDefaultRequestListener(request -> request.setHost("http://192.168.1.151:25555/BeamNGTelemetry"))
|
|
||||||
.addOnErrorDefaultListener(new EasyNet.OnErrorDefaultListenerWithCode(404) {
|
|
||||||
@Override
|
|
||||||
public void onError(NResponseModel responseModel) {
|
|
||||||
// For example, intercepted error 404
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
public OkHttpClient getNetworkClient() {
|
||||||
|
return networkClient;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,39 +1,99 @@
|
|||||||
package com.hakodev.androiditunesapi.activities;
|
package com.hakodev.androiditunesapi.activities;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.support.constraint.ConstraintLayout;
|
||||||
|
import android.support.design.widget.Snackbar;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.view.Menu;
|
import android.util.Log;
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.anthonycr.grant.PermissionsManager;
|
||||||
|
import com.anthonycr.grant.PermissionsResultAction;
|
||||||
|
import com.hakodev.androiditunesapi.AndroidItunesAPI;
|
||||||
import com.hakodev.androiditunesapi.R;
|
import com.hakodev.androiditunesapi.R;
|
||||||
|
import com.hakodev.androiditunesapi.util.Utils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.Callback;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class ArtistListActivity extends AppCompatActivity {
|
public class ArtistListActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
private final static String TAG = "ArtistListActivity";
|
||||||
|
private final static String SEARCH_URL = "https://itunes.apple.com/search?term=%1$s&country=US&entity=musicArtist&limit=10";
|
||||||
|
|
||||||
|
private OkHttpClient networkClient;
|
||||||
|
private ConstraintLayout lytBase;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setupTheme();
|
setupTheme();
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_artists_list);
|
||||||
|
setupViews();
|
||||||
|
init();
|
||||||
|
askForPermissions();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||||
getMenuInflater().inflate(R.menu.actionbar_search, menu);
|
PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
|
||||||
int id = item.getItemId();
|
|
||||||
if (id == R.id.action_search) {
|
|
||||||
Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupTheme() {
|
private void setupTheme() {
|
||||||
setTheme(R.style.AppTheme);
|
setTheme(R.style.AppTheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupViews() {
|
||||||
|
lytBase = findViewById(R.id.lytBase);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
networkClient = AndroidItunesAPI.getInstance().getNetworkClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void askForPermissions() {
|
||||||
|
//Not needed for internet permission but more of a proof of concept
|
||||||
|
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(this,
|
||||||
|
new String[]{Manifest.permission.INTERNET}, new PermissionsResultAction() {
|
||||||
|
@Override
|
||||||
|
public void onGranted() {
|
||||||
|
Log.d(TAG, "Permissions Granted");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDenied(String permission) {
|
||||||
|
Toast.makeText(ArtistListActivity.this, R.string.permissions_not_granted, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void requestArtist(String artistName) {
|
||||||
|
new Thread(() -> {
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(String.format(SEARCH_URL, Utils.formatStringForURL(artistName)))
|
||||||
|
.build();
|
||||||
|
networkClient.newCall(request)
|
||||||
|
.enqueue(new Callback() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(@NonNull final Call call, @NonNull IOException e) {
|
||||||
|
Log.e(TAG, e.getLocalizedMessage());
|
||||||
|
runOnUiThread(() -> Snackbar.make(lytBase, R.string.network_connection_error, Snackbar.LENGTH_LONG).show());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(@NonNull Call call, @NonNull final Response response) throws IOException {
|
||||||
|
String res = response.body().string();
|
||||||
|
Log.w(TAG, res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.google.gson.annotations.SerializedName;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Artist {
|
public class Response {
|
||||||
|
|
||||||
@SerializedName("resultCount")
|
@SerializedName("resultCount")
|
||||||
@Expose
|
@Expose
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.hakodev.androiditunesapi.util;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
public class Utils {
|
||||||
|
public static String formatStringForURL(String text) {
|
||||||
|
String encodedText = "";
|
||||||
|
try {
|
||||||
|
return URLEncoder.encode(text, "UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return encodedText;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
app/src/main/res/drawable/default_artist.png
Normal file
BIN
app/src/main/res/drawable/default_artist.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
39
app/src/main/res/layout/activity_artists_list.xml
Normal file
39
app/src/main/res/layout/activity_artists_list.xml
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<android.support.constraint.ConstraintLayout 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"
|
||||||
|
android:id="@+id/lytBase"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
tools:context="com.hakodev.androiditunesapi.activities.ArtistListActivity">
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/txtArtistSearch"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginEnd="8dp"
|
||||||
|
android:layout_marginStart="8dp"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:drawableEnd="@drawable/ic_search"
|
||||||
|
android:drawableRight="@drawable/ic_search"
|
||||||
|
android:ems="10"
|
||||||
|
android:hint="@string/search_artist"
|
||||||
|
android:inputType="textPersonName"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<ListView
|
||||||
|
android:id="@+id/listView"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_marginBottom="8dp"
|
||||||
|
android:layout_marginEnd="8dp"
|
||||||
|
android:layout_marginStart="8dp"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/txtArtistSearch" />
|
||||||
|
|
||||||
|
</android.support.constraint.ConstraintLayout>
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<android.support.constraint.ConstraintLayout 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"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
tools:context="com.hakodev.androiditunesapi.activities.ArtistListActivity">
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="Hello World!"
|
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
|
||||||
app:layout_constraintLeft_toLeftOf="parent"
|
|
||||||
app:layout_constraintRight_toRightOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
|
||||||
|
|
||||||
</android.support.constraint.ConstraintLayout>
|
|
||||||
34
app/src/main/res/layout/artists_list_adapter.xml
Normal file
34
app/src/main/res/layout/artists_list_adapter.xml
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="48dp"
|
||||||
|
android:layout_gravity="center_vertical">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/imgArtist"
|
||||||
|
android:layout_width="36dp"
|
||||||
|
android:layout_height="36dp"
|
||||||
|
android:layout_marginBottom="8dp"
|
||||||
|
android:layout_marginStart="8dp"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:layout_marginLeft="8dp"
|
||||||
|
android:contentDescription="@string/description_artists_image"
|
||||||
|
android:src="@drawable/default_artist"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/lblArtistName"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="8dp"
|
||||||
|
android:layout_marginEnd="8dp"
|
||||||
|
android:layout_marginStart="8dp"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
</android.support.constraint.ConstraintLayout>
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
|
||||||
<item
|
|
||||||
android:id="@+id/action_search"
|
|
||||||
android:icon="@drawable/ic_search"
|
|
||||||
android:orderInCategory="100"
|
|
||||||
android:title="@string/search"
|
|
||||||
app:showAsAction="always" />
|
|
||||||
</menu>
|
|
||||||
@@ -6,4 +6,5 @@
|
|||||||
<color name="colorBackground">#111111</color>
|
<color name="colorBackground">#111111</color>
|
||||||
<color name="colorText">#ffffff</color>
|
<color name="colorText">#ffffff</color>
|
||||||
<color name="colorTextSecondary">#e4ded7</color>
|
<color name="colorTextSecondary">#e4ded7</color>
|
||||||
|
<color name="colorHintText">#838383</color>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
<resources>
|
<resources>
|
||||||
<!-- Common terms -->
|
<!-- Common terms -->
|
||||||
<string name="app_name">Artist Vault</string>
|
<string name="app_name">Artist Vault</string>
|
||||||
<string name="search">Buscar</string>
|
<string name="search_artist">Buscar artista</string>
|
||||||
|
|
||||||
|
<!-- Permissions -->
|
||||||
|
<string name="permissions_not_granted">No tengo los permisos necesarios para funcionar</string>
|
||||||
|
|
||||||
|
<!-- Networking -->
|
||||||
|
<string name="network_connection_error">Error en la conexión</string>
|
||||||
|
|
||||||
|
<!-- Image descriptions -->
|
||||||
|
<string name="description_artists_image">Imágen del artista</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
<item name="android:windowBackground">@color/colorBackground</item>
|
<item name="android:windowBackground">@color/colorBackground</item>
|
||||||
<item name="android:textColor">@color/colorText</item>
|
<item name="android:textColor">@color/colorText</item>
|
||||||
<item name="android:textColorSecondary">@color/colorTextSecondary</item>
|
<item name="android:textColorSecondary">@color/colorTextSecondary</item>
|
||||||
|
<item name="android:textColorHint">@color/colorHintText</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style name="AppTheme.NoActionBar">
|
<style name="AppTheme.NoActionBar">
|
||||||
|
|||||||
4
app/src/main/res/xml/backup_rules.xml
Normal file
4
app/src/main/res/xml/backup_rules.xml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<full-backup-content>
|
||||||
|
<include domain="sharedpref" path="."/>
|
||||||
|
</full-backup-content>
|
||||||
Reference in New Issue
Block a user