Imports don't work in android studio (java file) - android-studio

This 3 imports(in the img) looks like they are in comment and don't work. I tried to clear and rebuild project several times but it doesn't help. I don't know what to do.
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
...
#Override
public boolean onMenuItemClick(Menultem item) {
switch (item.getItemId()) {
case R.id.action_profile:
return true;
case R.id.action_articles:
return true;
case R.id.action_about:
return true;
default:
return false;
}
}
Ow and MenuItem with .getItemId in code tigger errors

Related

What is the argument matcher for a Mono type parameter?

Class under test:
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
public class ServiceProcessorImpl implements ServiceProcessor {
#Autowired
private WebClient webClient;
#Override
public Mono<ServiceResponse> fetchOffersByItemIdAndStoreId(Mono<List<PriceAndOfferRequest>> list) {
String url = "<some url>";
Mono<ServiceResponse> response = webClient
.post()
.uri(url)
.headers(httpHeaders -> getHeaders(httpHeaders))
.body(list,List.class)
.retrieve()
.onStatus(HttpStatus::isError, clientResponse -> {
log.error("Error on API Calling : {}", clientResponse.statusCode());
return Mono.empty();
})
.bodyToMono(PnOServiceResponse.class);
return response;
}
}
Test Case:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
#RunWith(SpringRunner.class)
public class ServiceProcessorImplTest {
#Mock
private static WebClient webClientMock;
#Mock
private WebClient.RequestHeadersSpec requestHeadersSpecMock;
#Mock
private WebClient.RequestHeadersUriSpec requestHeadersUriSpecMock;
#Mock
private WebClient.RequestBodySpec requestBodySpecMock;
#Mock
private WebClient.RequestBodyUriSpec requestBodyUriSpecMock;
#Mock
private WebClient.ResponseSpec responseSpecMock;
#InjectMocks
static ServiceProcessorImpl pnOServiceProcessor;
#Test
public void test_whenErrorFromService_thenMonoEmpty() {
Mockito.when(webClientMock.post()).thenReturn(requestBodyUriSpecMock);
Mockito.when(requestBodyUriSpecMock.uri(Mockito.anyString())).thenReturn(requestBodySpecMock);
Mockito.when(requestBodySpecMock.headers(Mockito.any())).thenReturn(requestBodySpecMock);
Mockito.when(requestBodySpecMock.body(Mockito.any(Mono.class), Mockito.any(Class.class))).thenReturn(requestHeadersSpecMock);
Mockito.when(requestHeadersSpecMock.retrieve()).thenReturn(responseSpecMock);
Mockito.when(responseSpecMock.onStatus(Mockito.any(), Mockito.any())).thenReturn(responseSpecMock);
Mockito.when(responseSpecMock.bodyToMono(Mockito.eq(PnOServiceResponse.class))).thenReturn(Mono.empty());
Mono<List<PriceAndOfferRequest>> reqList = createPriceAndOfferRequestList();
Mono<PnOServiceResponse> pnOServiceResponseMono = pnOServiceProcessor.fetchOffersByItemIdAndStoreId(reqList);
StepVerifier.create(pnOServiceResponseMono)
.expectNextCount(0)
.verifyComplete();
}
private Mono<List<PriceAndOfferRequest>> createPriceAndOfferRequestList() {
PriceAndOfferRequest pnoRequest = new PriceAndOfferRequest();
pnoRequest.setItemId("12345");
pnoRequest.setStoreNumber("store12345");
List<PriceAndOfferRequest> list = Arrays.asList(pnoRequest);
return Mono.just(list);
}
private Mono<PnOServiceResponse> createMockPnOResponse() {
PnOServiceResponse pnOServiceResponse = new PnOServiceResponse();
OfferPriceDTO offerPriceDTO = new OfferPriceDTO();
PnOItem item = new PnOItem();
item.setItemId("12345");
offerPriceDTO.setItems(Arrays.asList(item));
pnOServiceResponse.setOfferPriceDTO(offerPriceDTO);
return Mono.just(pnOServiceResponse);
}
}
The code gives NullPointerException while calling .body() on the webClient in fetchOffersByItemIdAndStoreId method. Looks like there is some issue with the ArgumentMatcher in the test case when mocking the call to body() in Mockito.when(requestBodySpecMock.body(Mockito.any(Mono.class), Mockito.any(Class.class))).thenReturn(requestHeadersSpecMock);
Resolved.
Mockito.any(Object.class) worked me.
Mockito.when(requestBodySpecMock.body(Mockito.any(Object.class), Mockito.any(Class.class))).thenReturn(requestHeadersSpecMock);
You can use the generic matcher ArgumentMatchers.any()

save last position of videoView saved by sharedprefrence and start in last position (android studio)

save last position of videoView saved by sharedprefrence and start in last position (android studio)
hello I serch for my problam in stackaverflow and other refrences but I dont get my answer
.
I use VideoView In my app
I want to save the last position of videview in a sharedPrefrence to start again from the last position but it start in 0 every time
please check my code and tel me what should i do.
package com.rewass.qurankurdishaudiotranslatev2.activitys;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.rewass.qurankurdishaudiotranslatev2.R;
import com.rewass.qurankurdishaudiotranslatev2.adapters.RecyclerViewAdapter;
import com.rewass.qurankurdishaudiotranslatev2.model.ModelRecycler;
import com.rewass.qurankurdishaudiotranslatev2.text.Text002;
import java.util.ArrayList;
import java.util.List;
public class A002 extends AppCompatActivity {
List<ModelRecycler> listItems;
private InterstitialAd mInterstitialAd;
String AudioURL = "https://download.quranicaudio.com/quran/mishaari_raashid_al_3afaasee/002.mp3";
VideoView videoView;
ProgressBar progressBar008;
TextView te008;
int progress;
RecyclerView myrv;
String kurdish002 = "http://sirwaan.com/aaap/audios/kurdish/sound/quran/qurankurdish/002.mp3";
String v2kurdish002 = "https://sirwaan.com/aaap/audios/kurdish/sound/quran/newversionqurankurdish/002.mp3";
Handler handler;
GridLayoutManager layoutManager;
int seekkk;
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a002);
audioSora();
}
SharedPreferences preferences ;
private void audioSora() {
preferences = getSharedPreferences("pref55",MODE_PRIVATE);
videoView = findViewById(R.id.videoview002);
videoView.setVideoPath(v2kurdish002);
//control media
MediaController mediaController = new MediaController(this);
//set view with controller
videoView.setMediaController(mediaController);
//set Controller to view
mediaController.setAnchorView(videoView);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
SharedPreferences.Editor editor = preferences.edit();
int progress = videoView.getCurrentPosition();
editor.putInt("seekto", progress);
editor.apply();
seekkk = preferences.getInt("seekto", 0);
Toast.makeText(A002.this, seekkk+"", Toast.LENGTH_SHORT).show();
handler.postDelayed(this, 1000); }};
handler.postDelayed(r, 1000);
videoView.seekTo(seekkk);
videoView.start();
}
});
}
}
private int time = 0;
private ScheduledExecutorService mScheduledExecutorService;
Runnable getPositionVideo = () -> {
time = binding.epVideoView.getCurrentPosition();
Log.d(TAG, ": " + time);
};
binding.epVideoView.setOnPreparedListener(mediaPlayer -> {
mediaController.setAnchorView(binding.epVideoView);
binding.epVideoView.start();
mScheduledExecutorService = new ScheduledThreadPoolExecutor(1);
mScheduledExecutorService.scheduleWithFixedDelay(() -> {
runOnUiThread(getPositionVideo);
MediaControllerCustom.init(url, time);
}, 1000, 1000, TimeUnit.MILLISECONDS);
});

CDI #Decorator, Is there a way to deactivate a decorator at runtime?

I use the following workaround in order to control the behaviour of a #Decorator since I couldn't find a way to deactivate it.
if (!FacesContext.getCurrentInstance().getViewRoot().getViewId()
.endsWith("decoratorDemo.xhtml")) {
return transInterBean.getTransactionalInsertRecords();
} else {
...
}
Is there no way to decide at runtime whether a decorator should be applied?
package com.cdi.decorators;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.inject.Any;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import com.cdi.cdibeans.TransactionalInterceptor;
import com.cdi.cdibeans.TransactionalInterceptorBean;
#Decorator
public abstract class TransactionalInterceptorDecorator implements
TransactionalInterceptor {
/**
*
*/
private static final long serialVersionUID = -1191671082441891759L;
#Inject
#Delegate
#Any
TransactionalInterceptorBean transInterBean;
#Override
public ArrayList<String> getTransactionalInsertRecords()
throws SQLException {
ArrayList<String> records = new ArrayList<String>();
if (!FacesContext.getCurrentInstance().getViewRoot().getViewId()
.endsWith("decoratorDemo.xhtml")) {
return transInterBean.getTransactionalInsertRecords();
} else {
Iterator<String> iter = transInterBean
.getTransactionalInsertRecords().iterator();
while (iter.hasNext()) {
String record = iter.next();
records.add(record);
records.add(">>>Decorator<<< Added record ... ");
}
if (records.isEmpty()) {
records.add(">>>Decorator<<< Currently there are no records yet!");
}
return records;
}
}
}
Deltaspike has an exclude feature ... may be this could help, I didn't try it with decorators.

How to auto-scroll to the end in WebView?

I've problem with Scrollbar in WebView. I want to when the web view loaded, the srollbar auto scroll to the end of web view.
How can I do it?
This is my sample
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.ScrollBar;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Set;
public class Controller implements Initializable {
#FXML
WebView webView;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
loadWebView();
}
private void loadWebView() {
webView.setStyle("-fx-background-color: #cccccc; -fx-border-color: #00a7c8");
StringBuilder html= new StringBuilder().append("<html><body>");
for (int i=0; i<100; i++) {
html.append("<h1>" + i + ". AAAAAAAAAAAA</h1></br>");
}
html.append("</body></html>");
Set<Node> nodes = webView.lookupAll(".scroll-bar");
for (Node node : nodes) {
if (ScrollBar.class.isInstance(node)) {
System.out.println("Scrollbar here!!!");
ScrollBar scroll = (ScrollBar) node;
scroll.setValue(scroll.getMax());
}
}
WebEngine webEngine = webView.getEngine();
webEngine.loadContent(html.toString());
}
}
Thanks 4 your reading
From https://forums.oracle.com/message/11243184#11243184 :
package sample;
import com.sun.javafx.scene.control.skin.ScrollBarSkin;
import com.sun.webpane.sg.theme.ScrollBarThemeImpl;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.TextArea;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Set;
public class Controller implements Initializable {
#FXML
WebView webView;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
loadWebView();
}
private void loadWebView() {
webView.setStyle("-fx-background-color: #cccccc; -fx-border-color: #00a7c8");
StringBuilder html= new StringBuilder().append("<html>");
html.append("<head>");
html.append(" <script language=\"javascript\" type=\"text/javascript\">");
html.append(" function toBottom(){");
html.append(" window.scrollTo(0, document.body.scrollHeight);");
html.append(" }");
html.append(" </script>");
html.append("</head>");
html.append("<body onload='toBottom()'>");
for (int i=0; i<100; i++) {
html.append("<h1>" + i + ". AAAAAAAAAAAA</h1></br>");
}
html.append("</body></html>");
WebEngine webEngine = webView.getEngine();
webEngine.loadContent(html.toString());
}
}
Simply execute this script after your loadContent call:
webEngine.executeScript("window.scrollTo(0, document.body.scrollHeight);");
You have to wait until the Worker has finished to then proceed tp scroll down.
webView.getEngine().loadContent("<p>my text</p>");
webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
#Override
public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) {
if (newValue == Worker.State.SUCCEEDED)
webView.getEngine().executeScript("window.scrollTo(0, document.body.scrollHeight);");
}
});

Can't get custom sound on toggle button

Trying to get my toggle buttons to make custom sounds but I just can't get it, I'm very new to eclipse and I managed to get it on the splash screen but can't get it for the buttons.
package org.iimed.www;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ToggleButton;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class vpbox extends Activity {
private ImageButton home;
public void onCreate(Bundle vpbox_activity) {
super.onCreate(vpbox_activity);
setContentView(R.layout.vpbox_activity);
tuetoggle = (ToggleButton) findViewById(R.id.tuetoggle);
home = (ImageButton) findViewById(R.id.home);
home.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
startActivity(new Intent(vpbox.this, MainActivity.class));
ToggleButton sb = (ToggleButton) findViewById(R.id.suntoggle);
sb.setText(null);
sb.setTextOn(null);
sb.setTextOff(null);
sb.setBackgroundResource(R.drawable.vpbuttons);
ToggleButton mt= (ToggleButton)findViewById (R.id.montoggle);
mt.setText(null);
mt.setTextOn(null);
mt.setTextOff(null);
mt.setBackgroundResource(R.drawable.vpmonday);
ToggleButton wt = (ToggleButton) findViewById(R.id.wedtoggle);
wt.setText(null);
wt.setTextOff(null);
wt.setTextOn(null);
wt.setBackgroundResource(R.drawable.vpwed);
ToggleButton ft = (ToggleButton) findViewById(R.id.fritoggle);
ft.setText(null);
ft.setTextOn(null);
ft.setTextOff(null);
ft.setBackgroundResource(R.drawable.vpfri);
The test button with the sound:
ToggleButton thb = (ToggleButton) findViewById(R.id.thutoggle);
thb.setText(null);
thb.setTextOn(null);
thb.setTextOff(null);
thb.setBackgroundResource(R.drawable.vpthu);
MediaPlayer mp= MediaPlayer.create(getBaseContext(), R.raw.splashsound);
mp.start();

Resources