Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TV mode support crash #626

Merged
merged 5 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.amahi.anywhere.activity;

import android.content.Intent;

import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
Expand All @@ -24,6 +26,8 @@ public class AuthenticationActivityTest {

@Test
public void testIsErrorMessageDisplayed_UsernameOrPasswordIsEmpty() {
authenticationActivityTestRule.getActivity().sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

onView(withId(R.id.username_layout)).check(matches(isDisplayed()));
onView(withId(R.id.password_layout)).check(matches(isDisplayed()));

Expand Down
11 changes: 3 additions & 8 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.amahi.anywhere"
android:installLocation="auto">
android:installLocation="auto"
tools:ignore="MissingLeanbackLauncher">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
Expand Down Expand Up @@ -70,13 +71,7 @@

<activity
android:name=".tv.activity.MainTVActivity"
android:theme="@style/Theme.Leanback.Browse">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
android:theme="@style/Theme.Leanback.Browse" />
<activity
android:name=".activity.SplashActivity"
android:theme="@style/SplashTheme">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.amahi.anywhere.server.client.ServerClient;
import org.amahi.anywhere.server.model.ServerFile;
import org.amahi.anywhere.server.model.ServerShare;
import org.amahi.anywhere.util.Constants;
import org.amahi.anywhere.util.Downloader;
import org.amahi.anywhere.util.Mimes;
import org.amahi.anywhere.util.Preferences;
Expand Down Expand Up @@ -144,7 +145,7 @@ private File getOfflineFilePath(String name, Context context) {
}

private Uri getImageUri(Context context, ServerFile file) {
if (!Preferences.getServerName(context).equals("Welcome to Amahi")) {
if(!Preferences.getServerName(context).equals(Constants.welcomeToAmahi)) {
return serverClient.getFileThumbnailUri(serverShare, file);
}
return serverClient.getFileUri(serverShare, file);
Expand Down
64 changes: 45 additions & 19 deletions src/main/java/org/amahi/anywhere/fragment/NavigationFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@
import android.accounts.OnAccountsUpdateListener;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.app.UiModeManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -47,6 +45,11 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.squareup.otto.Subscribe;

import org.amahi.anywhere.AmahiApplication;
Expand All @@ -69,7 +72,8 @@
import org.amahi.anywhere.server.client.ServerClient;
import org.amahi.anywhere.server.model.Server;
import org.amahi.anywhere.tv.activity.MainTVActivity;
import org.amahi.anywhere.util.CheckTV;
import org.amahi.anywhere.util.Constants;
import org.amahi.anywhere.util.Intents;
import org.amahi.anywhere.util.MultiSwipeRefreshLayout;
import org.amahi.anywhere.util.Preferences;
import org.amahi.anywhere.util.RecyclerItemClickListener;
Expand All @@ -82,6 +86,8 @@

import javax.inject.Inject;

import static android.content.Context.UI_MODE_SERVICE;

/**
* Navigation fragments. Shows main application sections and servers list as well.
*/
Expand Down Expand Up @@ -247,16 +253,20 @@ private void setUpServersState(Bundle state) {

setUpServersContent(servers);

if (checkIsATV()) {
launchTV(servers);
}

showContent();
}

private void setUpServersContent(List<Server> servers) {
if (!CheckTV.isATV(mContext)) {
if (!checkIsATV()) {
replaceServersList(filterActiveServers(servers));
} else {
serversList = filterActiveServers(servers);
String serverName = Preferences.getPreference(mContext).getString(getString(R.string.pref_server_select_key), serversList.get(0).getName());

if (serverName == null) serverName = Constants.welcomeToAmahi;
if (serversList.get(0).getName().matches(serverName))
replaceServersList(serversList);

Expand Down Expand Up @@ -310,11 +320,20 @@ private void showContent() {
ViewDirector.of(this, R.id.animator_content).show(R.id.layout_content);
}

private boolean checkIsATV() {
if (mContext == null)
return false;
UiModeManager uiModeManager = (UiModeManager) mContext.getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION)
return true;
return false;
}

private void setUpAuthentication() {
if (getAccounts().isEmpty()) {
setUpAccount();
} else {
if (Preferences.getFirstRun(mActivity) && !CheckTV.isATV(mActivity)) {
if (Preferences.getFirstRun(mActivity) && !checkIsATV()) {
launchIntro();
}
setUpAuthenticationToken();
Expand Down Expand Up @@ -345,9 +364,9 @@ public void onServersLoaded(ServersLoadedEvent event) {

showContent();

tvIntent = new Intent(mContext, MainTVActivity.class);

tvIntent.putParcelableArrayListExtra("INTENT_SERVERS", new ArrayList<>(filterActiveServers(event.getServers())));
if (checkIsATV() && serverClient.isConnected()) {
launchTV(event.getServers());
}
}

private SwipeRefreshLayout getRefreshLayout() {
Expand Down Expand Up @@ -542,17 +561,18 @@ private void showOfflineNavigation() {
getOfflineFilesLayout().setVisibility(View.VISIBLE);
getRecentFilesLayout().setVisibility(View.VISIBLE);

getOfflineFilesLayout().setOnClickListener(
view ->
showOfflineFiles()
);
getOfflineFilesLayout().setOnClickListener(view -> showOfflineFiles());

areServersVisible = false;
setUpNavigationList();
getLinearLayoutSelectedServer().setOnClickListener((v) -> {
Toast.makeText(mContext, R.string.message_connection_error, Toast.LENGTH_SHORT).show();
});

if (checkIsATV()) {
launchTV(new ArrayList<>()); // Offline Navigation. No Server Available
}

showContent();
Toast.makeText(mContext, R.string.message_connection_error, Toast.LENGTH_SHORT).show();
}
Expand Down Expand Up @@ -581,10 +601,13 @@ private String getServerName() {
@Subscribe
public void onServerConnected(ServerConnectedEvent event) {
setUpServerConnection();
setUpNavigationList();
showContent();

if (CheckTV.isATV(mContext)) launchTV();
if (checkIsATV()) {
launchTV(getServersList());
} else {
setUpNavigationList();
showContent();
}
}

private void setUpServerConnection() {
Expand All @@ -600,8 +623,11 @@ private void setUpServerConnection() {
}
}

private void launchTV() {
private void launchTV(List<Server> serversList) {
tvIntent = new Intent(mContext, MainTVActivity.class);
tvIntent.putParcelableArrayListExtra(Intents.Extras.INTENT_SERVERS, new ArrayList<>(filterActiveServers(serversList)));
startActivity(tvIntent);
mActivity.finish();
}

private boolean isConnectionAvailable() {
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/org/amahi/anywhere/tv/activity/MainTVActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.amahi.anywhere.activity.NavigationActivity;
import org.amahi.anywhere.server.model.Server;
import org.amahi.anywhere.tv.fragment.MainTVFragment;
import org.amahi.anywhere.util.Intents;

import java.util.ArrayList;

Expand All @@ -48,7 +49,7 @@ private void checkAndLaunch() {
}

private ArrayList<Server> getServers() {
return getIntent().getParcelableArrayListExtra("INTENT_SERVERS");
return getIntent().getParcelableArrayListExtra(Intents.Extras.INTENT_SERVERS);
}

private void replaceFragment() {
Expand All @@ -58,17 +59,4 @@ private void replaceFragment() {
private void launchNav() {
startActivity(new Intent(this, NavigationActivity.class));
}

@Override
public void onBackPressed() {
super.onBackPressed();
startHomeIntent();
}

private void startHomeIntent() {
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ public class IconHeaderPresenter extends RowHeaderPresenter {

private float mUnselectedAlpha;

private Context ctx;

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
mUnselectedAlpha = parent.getResources()
.getFraction(R.fraction.lb_browse_header_unselect_alpha, 1, 1);

ctx = parent.getContext();

LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Expand All @@ -60,7 +64,7 @@ public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
rootView.setFocusable(true);
ImageView imageView = rootView.findViewById(R.id.header_icon);

if (headerItem.getName().matches(Resources.getSystem().getString(R.string.settings))) {
if (headerItem.getName().matches(ctx.getString(R.string.settings))) {
imageView.setVisibility(View.VISIBLE);
Drawable icon = ContextCompat.getDrawable(rootView.getContext(), R.drawable.ic_menu_settings);
imageView.setImageDrawable(icon);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/amahi/anywhere/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ public class Constants {

public static final String sharesPathAppendLoc = "shares";

public static final String welcomeToAmahi = "Welcome to Amahi";

public static final String amahiAndroidUrl = "https://www.amahi.org/android";
}
1 change: 1 addition & 0 deletions src/main/java/org/amahi/anywhere/util/Intents.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static final class Extras {
public static final String IMAGE_URIS = "image_uris";
public static final String UNIQUE_KEY = "unique_key";
public static final String FILE_TYPE = "file_type";
public static final String INTENT_SERVERS = "INTENT_SERVERS";

private Extras() {
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@
<string name="disable">Désactiver</string>
<string name="header_settings">Paramètres</string>
<string name="settings">Paramètres</string>
<string name="songs_in">Chanson (s) en</string>
<string name="songsin">Chanson (s) en</string>
<string name="videos_in">Vidéo (s) en</string>
<string name="videosin">Vidéo (s) en</string>
<string name="songs_in">Chanson (s) en </string>
<string name="songsin">Chanson (s) en </string>
<string name="videos_in">Vidéo (s) en </string>
<string name="videosin">Vidéo (s) en </string>
<string name="preference_title_about_intro">Intro</string>
<string name="preference_title_account_sign_out">Déconnexion</string>
<string name="preference_title_server_connection">Lien</string>
Expand Down
1 change: 0 additions & 1 deletion src/main/res/values-hi-rIN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<string name="pref_title_server_select_desc">वह सर्वर चुनें जिसके शेयरों को आप एक्सेस करना चाहते हैं</string>
<string name="pref_title_server_active_list">सक्रिय सर्वरों की सूची</string>
<string name="pref_server_active_list_desc">सक्रिय सर्वर चुनें, जिसके शेयर आप एक्सेस करना चाहते हैं</string>
<string name="pref_server_select_key">सर्वर का चयन करें</string>
<string name="pref_title_sign_out">साइन आउट</string>
<string name="pref_title_account">लेखा</string>
<string name="pref_sign_out_desc">अमाही टीवी से साइन आउट करें</string>
Expand Down
1 change: 0 additions & 1 deletion src/main/res/values-ml-rIN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@
<string name="pref_title_server_select_desc">നിങ്ങൾ ആക്സസ് ചെയ്യാൻ ആഗ്രഹിക്കുന്ന സെർവറിനെ തിരഞ്ഞെടുക്കുക.</string>
<string name="pref_title_server_active_list">സജീവ സെർവറുകളുടെ ലിസ്റ്റ്</string>
<string name="pref_server_active_list_desc">നിങ്ങൾ ആക്സസ് ചെയ്യാൻ ആഗ്രഹിക്കുന്ന സജീവ സെർവർ തിരഞ്ഞെടുക്കുക.</string>
<string name="pref_server_select_key">select_server</string>
<string name="pref_title_sign_out">സൈൻ ഔട്ട്</string>
<string name="pref_title_account">അക്കൗണ്ട്</string>
<string name="pref_sign_out_desc">അമാഹി ടിവിയിൽ നിന്ന് സൈൻ ഔട്ട് ചെയ്യുക</string>
Expand Down
8 changes: 4 additions & 4 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@
<string name="disable">Disable</string>
<string name="settings">Settings</string>
<string name="header_settings">Settings</string>
<string name="songs_in">Song(s) in</string>
<string name="songsin">Song(s) in</string>
<string name="videos_in">Video(s) in</string>
<string name="videosin">Video(s) in</string>
<string name="songs_in">Song(s) in </string>
<string name="songsin">Song(s) in </string>
<string name="videos_in">Video(s) in </string>
<string name="videosin">Video(s) in </string>

<string name="alert_delete_dialog">Delete file</string>
<string name="alert_delete_confirm">Are you sure?</string>
Expand Down